Advanced testing in Spring Boot involves using sophisticated tools and techniques to ensure application reliability and performance. This article explores various testing approaches and their implementation.
Key features include:
@SpringBootTest
@AutoConfigureMockMvc
@Testcontainers
public class UserServiceIntegrationTest {
@Container
static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:13");
@Autowired
private UserService userService;
@DynamicPropertySource
static void postgresProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}
@Test
void shouldCreateUser() {
User user = new User("test@example.com", "password");
User saved = userService.createUser(user);
assertThat(saved.getId()).isNotNull();
}
}
@SpringBootTest
@AutoConfigureStubRunner
public class ContractTest {
@StubRunnerPort("user-service")
int userServicePort;
@Test
void shouldGetUser() {
RestTemplate restTemplate = new RestTemplate();
User user = restTemplate.getForObject(
"http://localhost:" + userServicePort + "/api/users/1",
User.class
);
assertThat(user).isNotNull();
}
}
// Contract definition
Contract.make {
request {
method 'GET'
url '/api/users/1'
}
response {
status 200
body([
id: 1,
email: 'test@example.com',
name: 'Test User'
])
}
}
@SpringBootTest
public class ExternalServiceTest {
private MockWebServer mockWebServer;
@BeforeEach
void setup() throws IOException {
mockWebServer = new MockWebServer();
mockWebServer.start();
}
@AfterEach
void tearDown() throws IOException {
mockWebServer.shutdown();
}
@Test
void shouldCallExternalService() {
mockWebServer.enqueue(new MockResponse()
.setBody("{\"id\": 1, \"name\": \"Test\"}")
.addHeader("Content-Type", "application/json"));
ExternalService service = new ExternalService(mockWebServer.url("/").toString());
Response response = service.getData();
assertThat(response.getId()).isEqualTo(1);
}
}
Issue | Solution |
---|---|
Container startup issues | Check Docker daemon and container logs |
Contract test failures | Verify contract definitions and service responses |
Mock server errors | Check request/response matching |
Advanced testing in Spring Boot requires understanding various tools and techniques. Proper implementation of Testcontainers, Contract Testing, and MockWebServer is crucial for reliable tests.
Remember to follow best practices, handle test data properly, and maintain test performance for efficient development.