Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions backend/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,21 @@ class FooServiceTest {
```

### Integration Tests (`*IntegrationTest.java`)

All integration tests extend `AbstractIntegrationTest`, which provides:
- **Shared containers** — `SharedContainers` starts a single MySQL and Mailpit container per JVM (singleton pattern), shared by all test classes
- **`@Transactional` rollback** — each test runs in a transaction that rolls back automatically, restoring the DataInitializer-seeded state without needing `@DirtiesContext`
- **Common annotations** — `@SpringBootTest`, `@AutoConfigureMockMvc`, `@ActiveProfiles("dev")`, `@Tag("integration")`

```java
@SpringBootTest
@Testcontainers
@AutoConfigureMockMvc
@DisplayName("Integration tests for Foo API endpoints")
@Tag("integration")
@ActiveProfiles(value = "dev")
public class FooIntegrationTest {
public class FooIntegrationTest extends AbstractIntegrationTest {
@Autowired MockMvc mockMvc;
@Autowired JsonMapper jsonMapper;

@Value("${api.endpoint.base-url}")
String baseUrl;

@Container @ServiceConnection
static MySQLContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:8.0"));

String adminToken;
String studentToken;

Expand All @@ -153,4 +151,4 @@ public class FooIntegrationTest {
}
```

Integration tests use Testcontainers (MySQL 8.0), seed data from `DataInitializer`, and authenticate via HTTP Basic to get JWT tokens for subsequent requests. Tests verify both the `flag` and `code` fields in the `Result` response. Use `@DirtiesContext` on tests that modify state and could affect other tests.
Integration tests use Testcontainers (MySQL 8.0), seed data from `DataInitializer`, and authenticate via HTTP Basic to get JWT tokens for subsequent requests. Tests verify both the `flag` and `code` fields in the `Result` response. Do **not** use `@DirtiesContext` — the `@Transactional` rollback on the base class handles database reset automatically.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package team.projectpulse;

import org.junit.jupiter.api.Tag;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.MySQLContainer;

/**
* Base class for all integration tests. Provides:
* <ul>
* <li>Shared MySQL and Mailpit containers via {@link SharedContainers} (started once per JVM)</li>
* <li>{@code @Transactional} rollback after each test — restores DataInitializer-seeded state
* without the cost of {@code @DirtiesContext} (which would recreate the entire Spring context)</li>
* <li>Common annotations: {@code @SpringBootTest}, {@code @AutoConfigureMockMvc},
* {@code @ActiveProfiles("dev")}, {@code @Tag("integration")}</li>
* </ul>
*/
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("dev")
@Tag("integration")
@Transactional
public abstract class AbstractIntegrationTest {

@ServiceConnection
static final MySQLContainer<?> mysql = SharedContainers.MYSQL;


@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry registry) {
registry.add("spring.mail.host", SharedContainers.MAILPIT::getHost);
registry.add("spring.mail.port", () -> SharedContainers.MAILPIT.getMappedPort(1025));
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
package team.projectpulse;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

@SpringBootTest
@Testcontainers
class ProjectPulseApplicationTests {

@Container
@ServiceConnection
static MySQLContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:8.0"));

class ProjectPulseApplicationTests extends AbstractIntegrationTest {

@Test
void contextLoads() {
Expand Down
27 changes: 27 additions & 0 deletions backend/src/test/java/team/projectpulse/SharedContainers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package team.projectpulse;

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;

/**
* Singleton containers shared by all integration tests. Started once per JVM — avoids
* spinning up a new MySQL/Mailpit container per test class.
*/
public final class SharedContainers {

public static final MySQLContainer<?> MYSQL = new MySQLContainer<>(DockerImageName.parse("mysql:8.0"));

public static final GenericContainer<?> MAILPIT = new GenericContainer<>(DockerImageName.parse("axllent/mailpit"))
.withExposedPorts(1025, 8025);

static {
MYSQL.start();
MAILPIT.start();
}


private SharedContainers() {
}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
package team.projectpulse.activity;

import tools.jackson.databind.json.JsonMapper;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import team.projectpulse.AbstractIntegrationTest;
import team.projectpulse.system.StatusCode;
import org.hamcrest.Matchers;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
Expand All @@ -36,13 +27,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@SpringBootTest
@Testcontainers
@AutoConfigureMockMvc
@DisplayName("Integration tests for Activity API endpoints")
@Tag("integration")
@ActiveProfiles(value = "dev")
public class ActivityIntegrationTest {
public class ActivityIntegrationTest extends AbstractIntegrationTest {

@Autowired
MockMvc mockMvc;
Expand All @@ -61,10 +47,6 @@ public class ActivityIntegrationTest {
@Value("${api.endpoint.base-url}")
String baseUrl;

@Container
@ServiceConnection
static MySQLContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:8.0"));

@BeforeEach
void setUp() throws Exception {
ResultActions resultActions = this.mockMvc.perform(post(this.baseUrl + "/users/login").with(httpBasic("b.wei@abc.edu", "123456"))); // httpBasic() is from spring-security-test.
Expand Down Expand Up @@ -228,7 +210,6 @@ void testStudentJanaFindActivityById() throws Exception {
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
void testStudentJohnAddActivity() throws Exception {
// Given
Map<String, Object> activityDto = new HashMap<>();
Expand Down Expand Up @@ -259,7 +240,6 @@ void testStudentJohnAddActivity() throws Exception {
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
void testStudentJohnUpdatesOwnActivity() throws Exception {
// Given
Map<String, Object> activityDto = new HashMap<>();
Expand Down Expand Up @@ -310,7 +290,6 @@ void testStudentUpdatesAnotherStudentsActivity() throws Exception {
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
void testStudentJohnDeletesOwnActivity() throws Exception {
this.mockMvc.perform(delete(this.baseUrl + "/activities/1").accept(MediaType.APPLICATION_JSON).header(HttpHeaders.AUTHORIZATION, this.studentJohnToken))
.andExpect(jsonPath("$.flag").value(true))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
package team.projectpulse.course;

import tools.jackson.databind.json.JsonMapper;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.MySQLContainer;
import team.projectpulse.AbstractIntegrationTest;
import team.projectpulse.system.StatusCode;
import org.hamcrest.Matchers;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -38,13 +26,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@SpringBootTest
@Testcontainers
@AutoConfigureMockMvc
@DisplayName("Integration tests for Course API endpoints")
@Tag("integration")
@ActiveProfiles(value = "dev")
class CourseIntegrationTest {
class CourseIntegrationTest extends AbstractIntegrationTest {

@Autowired
MockMvc mockMvc;
Expand All @@ -61,21 +44,6 @@ class CourseIntegrationTest {
@Value("${api.endpoint.base-url}")
String baseUrl;

@Container
@ServiceConnection
static MySQLContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:8.0"));

@Container
static final GenericContainer<?> mailpit = new GenericContainer<>(DockerImageName.parse("axllent/mailpit"))
.withExposedPorts(1025, 8025);


@DynamicPropertySource
static void setMailpitProperties(DynamicPropertyRegistry registry) {
registry.add("spring.mail.host", mailpit::getHost);
registry.add("spring.mail.port", () -> mailpit.getMappedPort(1025));
}

@BeforeEach
void setUp() throws Exception {
ResultActions resultActions = this.mockMvc.perform(post(this.baseUrl + "/users/login").with(httpBasic("b.wei@abc.edu", "123456"))); // httpBasic() is from spring-security-test.
Expand Down Expand Up @@ -185,7 +153,7 @@ void adminTimFindCourseById() throws Exception {
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)

void adminBingyangAddCourse() throws Exception {
// Given
Map<String, String> courseDto = new HashMap<>();
Expand Down Expand Up @@ -219,7 +187,7 @@ void instructorBillAddCourse() throws Exception {
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)

void adminBingyangUpdateCourse() throws Exception {
// Given
Map<String, String> courseDto = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
package team.projectpulse.evaluation;

import tools.jackson.databind.json.JsonMapper;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import team.projectpulse.AbstractIntegrationTest;
import team.projectpulse.evaluation.dto.PeerEvaluationDto;
import team.projectpulse.evaluation.dto.RatingDto;
import team.projectpulse.system.StatusCode;
import org.hamcrest.Matchers;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
Expand All @@ -41,13 +32,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@SpringBootTest
@Testcontainers
@AutoConfigureMockMvc
@DisplayName("Integration tests for Evaluation API endpoints")
@Tag("integration")
@ActiveProfiles(value = "dev")
public class EvaluationIntegrationTest {
public class EvaluationIntegrationTest extends AbstractIntegrationTest {

/**
* @MockitoSpyBean creates a Mockito spy and registers it as the active Spring bean.
Expand All @@ -74,10 +60,6 @@ public class EvaluationIntegrationTest {
@Value("${api.endpoint.base-url}")
String baseUrl;

@Container
@ServiceConnection
static MySQLContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:8.0"));


@BeforeEach
void setUp() throws Exception {
Expand All @@ -101,7 +83,7 @@ void setUp() throws Exception {
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)

void testStudentJohnAddEvaluation() throws Exception {
List<RatingDto> ratingDtos = List.of(
new RatingDto(null, 1, 4.0),
Expand Down Expand Up @@ -265,7 +247,7 @@ void testEvaluatorJohnGetsEvaluationsByWrongEvaluatorIdAndWeek() throws Exceptio
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)

void testEvaluatorJohnUpdatesOwnEvaluation() throws Exception {
List<RatingDto> ratingDtos = List.of(
new RatingDto(1, 1, 4.0),
Expand Down
Loading
Loading