Overview

Spring Boot offers strong built-in support for testing. The spring-boot-starter-test starter includes various testing features, helping developers write tests without complicated setup.

spring-boot-starter-test

  • Spring Boot provides the spring-boot-starter-test starter for integration testing
  • automatically includes key libraries such as JUnit 5, Mockito, AssertJ, Hamcrest, and Spring Test

Main libraries included:

LibraryDescriptionNotes
JUnit 5The leading Java testing frameworkUsing version 5 (which is different from 4). Version 3 is not used much.
- Ver 5 is the standard
- Explicitly state diff. version if you need to
MockitoCreates mock objectsUsed to disconnect (단절) layers for unit testing, slice testing
AssertJA library used for validation & gives assertion syntax.
- Provides readable, fluent assertion syntax
- Used together with JUnit. Friendly to developers
HamcrestMatcher-based assertion tool- alternative to AssertJ for assertions
- style is closer to english
- what to choose is just team/personal preference
Spring TestSupports Spring ApplicationContext and MockMvcEnables dependency injection (@Autowired) in tests by loading the Spring container (ApplicationContext)
- [[IoC (Inversion of Control)#ApplicationContext|IoC (Inversion of Control) - ApplicationContext]]
JSONassertUseful for comparing JSON responsesIntelligently compares two JSON strings, ignoring formatting and field order, to check if they are semantically the same
Spring Boot TestSupports integration testing in Spring Boot- Simplifies integration testing with @SpringBootTest.
- Provides “slice test” annotations (e.g., @WebMvcTest, @DataJpaTest) to test specific application layers quickly.

Key Testing Annotations

Basically ALL OF THESE ARE IMPORTANT AND YOU NEED TO KNOW EVERYTHING

Spring provides various annotations to simplify writing tests. Key annotations include:

AnnotationLibraryDescriptionCommon Test Type(s)
@SpringBootTestSpring Boot TestLoads the entire application context for testingIntegration, Functional
@WebMvcTestSpring Boot TestLoads only the web layer (e.g., Controller) for slice testingSlice (Web Layer)
@DataJpaTestSpring Boot TestLoads only the persistence layer (JPA) for slice testing
- Data Access (Repository) Layer Testing
Slice (Data Layer)
@MockBeanSpring Boot TestReplaces a Spring bean with a Mockito mock in the contextSlice, Integration
@SpyBeanSpring Boot TestWraps a real Spring bean with a Mockito spy in the contextSlice, Integration
@TestJUnit 5Marks a method as a test method that should be runAll (Unit, Slice, etc.)
@BeforeEachJUnit 5Runs a setup method before each @Test methodAll (Unit, Slice, etc.)
@AfterEachJUnit 5Runs a teardown method after each @Test methodAll (Unit, Slice, etc.)
  • @SpringBootTest
    • loads the entire application context, which can be slow.
    • Use it only when necessary, and prefer slice tests like @WebMvcTest or @DataJpaTest for faster, more focused testing.
  • JUnit 5 explains more about the key annotations

Example Code

@SpringBootTest
class OrderServiceTest {
 
    @Autowired
    private OrderService orderService;
 
    @Test
    void createOrder_ShouldSucceed() {
        // given
        OrderRequest request = new OrderRequest("Americano", 2);
 
        // when
        Order order = orderService.createOrder(request);
 
        // then
        assertThat(order.getQuantity()).isEqualTo(2);
    }
}