Basics Flashcards
Narrow vs Broad Integration Tests
Narrow integration tests that exercise only part of the application and use test doubles for some components or external services. Some call these component tests or service tests to make the distinction.
Broad integration tests that need the whole application running and exercise the application through UI or network calls. Some call these system tests or end-to-end tests to make the distinction.
Narrow Integration Tests
The Spring Boot test slices like @WebMvcTest or @DataJpaTest that we saw earlier are narrow integration tests. They only load part of the application context and allow mocking of unneeded dependencies.
Broad Integration Tests
Broad integration tests that need the whole application running and exercise the application through UI or network calls.
@SpringBootTest:
Indicates that the annotated class is the Spring Boot test.
The Loads the complete application context.
@RunWith(SpringRunner.class):
The Specifies the class to run the tests.
The SpringRunner is the alias for SpringJUnit4ClassRunner.
@MockBean
The Mocks a Spring Bean for testing purposes.
Useful for the isolating the unit of the code being tested.
@SpringBootTest
public class MyServiceTest {
@Autowired
private MyService myService;
@MockBean
private ExternalService externalService;
// Test methods go here
}
Example
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHelloEndpoint() throws Exception {
mockMvc.perform(get(“/hello”))
.andExpect(status().isOk())
.andExpect(content().string(“Hello, World!”));
}
}
@TestConfiguration
The @TestConfiguration is used to the specify additional configuration for the tests. It is often used to define @Bean methods for the test-specific beans.
@TestConfiguration
public class TestConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
Server Starting
By default, @SpringBootTest will not start a server. You can use the webEnvironment attribute of @SpringBootTest to further refine how your tests run:
MOCK(Default) : Loads a web ApplicationContext and provides a mock web environment. Embedded servers are not started when using this annotation.
ANDOM_PORT: Loads a WebServerApplicationContext and provides a real web environment. Embedded servers are started and listen on a random port.
DEFINED_PORT: Loads a WebServerApplicationContext and provides a real web environment. Embedded servers are started and listen on a defined port (from your application.properties) or on the default port of 8080.
NONE: Loads an ApplicationContext by using SpringApplication but does not provide any web environment (mock or otherwise)
Testing with a mock environment
By default, @SpringBootTest does not start the server. If you have web endpoints that you want to test against this mock environment, you can additionally configure MockMvc
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MockMvcExampleTests {
@Autowired private MockMvc mvc; @Test public void exampleTest() throws Exception { this.mvc.perform(get("/")).andExpect(status().isOk()) .andExpect(content().string("Hello World")); }
}
Testing with a mock environment 2 WebTestClient
Alternatively, you can configure a WebTestClient as shown in the following example:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebTestClient
public class MockWebTestClientExampleTests {
@Autowired private WebTestClient webClient; @Test public void exampleTest() { this.webClient.get().uri("/").exchange().expectStatus().isOk() .expectBody(String.class).isEqualTo("Hello World"); }
}
Testing with a running server - WebTestClient
If you need to start a full running server, we recommend that you use random ports. If you use @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT), an available port is picked at random each time your test runs.
For convenience, tests that need to make REST calls to the started server can additionally @Autowire a WebTestClient, which resolves relative links to the running server and comes with a dedicated API for verifying responses
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class RandomPortWebTestClientExampleTests {
@Autowired private WebTestClient webClient; @Test public void exampleTest() { this.webClient.get().uri("/").exchange().expectStatus().isOk() .expectBody(String.class).isEqualTo("Hello World"); }
}
Dependency Injection Support
With @SpringBootTest, beans defined in your Spring application context are injected into the test class, so you can work with @Autowired dependencies in your tests.
Web Environment Support
You can specify a web environment (e.g., MOCK, RANDOM_PORT, DEFINED_PORT, NONE) to test specific configurations:
MOCK (default): Starts a mock web environment.
RANDOM_PORT: Starts an embedded server on a random port for testing real HTTP interactions.
DEFINED_PORT: Uses the preconfigured port of the server.
NONE: No web environment setup.
Use Sparingly
@SpringBootTest tests can be slower due to loading the full context, so they’re often used sparingly in combination with lighter tests, like @WebMvcTest or @DataJpaTest, for more focused component testing.