Implement Spring Framework integration tests with TestContainers, Spring Test, and database fixtures
Test component interactions, database operations, and external API integrations using Spring testing framework with TestContainers.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
@Sql(scripts = "/test-data.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public class PoliticianIntegrationTest {
@Autowired
private PoliticianRepository repository;
@Test
void shouldCreatePoliticianViaApi() {
// Arrange
PoliticianRequest request = new PoliticianRequest("John", "Doe", "S");
// Act
ResponseEntity<Politician> response = restTemplate.postForEntity(
"/api/politicians",
request,
Politician.class
);
// Assert
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getBody()).isNotNull();
// Verify in database
Politician saved = repository.findById(response.getBody().getId()).orElseThrow();
assertThat(saved.getFirstName()).isEqualTo("John");
}
}
@Testcontainers
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
public class DatabaseIntegrationTest {
@ClassRule
public static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:18")
.withDatabaseName("cia_test")
.withUsername("test")
.withPassword("test");
@BeforeClass
public static void setupTestDatabase() {
System.setProperty("spring.datasource.url", postgres.getJdbcUrl());
System.setProperty("spring.datasource.username", postgres.getUsername());
System.setProperty("spring.datasource.password", postgres.getPassword());
}
@Test
public void shouldConnectToDatabase() {
assertThat(postgres.isRunning()).isTrue();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationContext.class, SecurityConfig.class})
@WebAppConfiguration
public class SecurityIntegrationTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
@Test
@WithMockUser(roles = "ADMIN")
public void shouldAllowAdminAccess() throws Exception {
mockMvc.perform(get("/api/admin/users"))
.andExpect(status().isOk());
}
@Test
public void shouldDenyUnauthenticatedAccess() throws Exception {
mockMvc.perform(get("/api/admin/users"))
.andExpect(status().isUnauthorized());
}
}
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer