diff --git a/backend/src/test/java/ddangkong/controller/BaseControllerTest.java b/backend/src/test/java/ddangkong/controller/BaseControllerTest.java index c7516b209..9568c379b 100644 --- a/backend/src/test/java/ddangkong/controller/BaseControllerTest.java +++ b/backend/src/test/java/ddangkong/controller/BaseControllerTest.java @@ -1,13 +1,18 @@ package ddangkong.controller; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; + +import ddangkong.support.extension.DatabaseCleanerExtension; import io.restassured.RestAssured; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.jdbc.Sql; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ExtendWith(DatabaseCleanerExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @Sql(scripts = "/init-test.sql") public abstract class BaseControllerTest { diff --git a/backend/src/test/java/ddangkong/domain/BaseRepositoryTest.java b/backend/src/test/java/ddangkong/domain/BaseRepositoryTest.java index 92a080765..9f35171ec 100644 --- a/backend/src/test/java/ddangkong/domain/BaseRepositoryTest.java +++ b/backend/src/test/java/ddangkong/domain/BaseRepositoryTest.java @@ -4,7 +4,6 @@ import org.springframework.test.context.jdbc.Sql; @DataJpaTest -@Sql(scripts = "/init-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) +@Sql(scripts = "/init-test.sql") public abstract class BaseRepositoryTest { - } diff --git a/backend/src/test/java/ddangkong/domain/balance/content/RoomContentRepositoryTest.java b/backend/src/test/java/ddangkong/domain/balance/content/RoomContentRepositoryTest.java index 67af2b4ec..91d270ebf 100644 --- a/backend/src/test/java/ddangkong/domain/balance/content/RoomContentRepositoryTest.java +++ b/backend/src/test/java/ddangkong/domain/balance/content/RoomContentRepositoryTest.java @@ -18,10 +18,10 @@ class 방의_최신_질문_조회 { @Test void 방의_가장_최신의_질문을_조회할_수_있다() { // given - Long recentContentId = 1L; + Long roomId = 1L; // when - RoomContent actual = roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(recentContentId).get(); + RoomContent actual = roomContentRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId).get(); // then assertThat(actual.getId()).isEqualTo(2L); diff --git a/backend/src/test/java/ddangkong/service/BaseServiceTest.java b/backend/src/test/java/ddangkong/service/BaseServiceTest.java index 614232a77..3b52b23de 100644 --- a/backend/src/test/java/ddangkong/service/BaseServiceTest.java +++ b/backend/src/test/java/ddangkong/service/BaseServiceTest.java @@ -1,9 +1,14 @@ package ddangkong.service; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; + +import ddangkong.support.extension.DatabaseCleanerExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.jdbc.Sql; -@SpringBootTest +@ExtendWith(DatabaseCleanerExtension.class) +@SpringBootTest(webEnvironment = WebEnvironment.NONE) @Sql(scripts = "/init-test.sql") public abstract class BaseServiceTest { } diff --git a/backend/src/test/java/ddangkong/support/extension/DatabaseCleanerExtension.java b/backend/src/test/java/ddangkong/support/extension/DatabaseCleanerExtension.java new file mode 100644 index 000000000..0d153f34a --- /dev/null +++ b/backend/src/test/java/ddangkong/support/extension/DatabaseCleanerExtension.java @@ -0,0 +1,47 @@ +package ddangkong.support.extension; + +import jakarta.persistence.EntityManager; +import java.util.List; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.transaction.support.TransactionTemplate; + +public class DatabaseCleanerExtension implements BeforeEachCallback { + + @Override + public void beforeEach(ExtensionContext extensionContext) { + ApplicationContext context = SpringExtension.getApplicationContext(extensionContext); + cleanup(context); + } + + private void cleanup(ApplicationContext context) { + EntityManager em = context.getBean(EntityManager.class); + TransactionTemplate transactionTemplate = context.getBean(TransactionTemplate.class); + + transactionTemplate.execute(action -> { + em.clear(); + truncateTables(em); + return null; + }); + } + + private void truncateTables(EntityManager em) { + em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate(); + for (String tableName : findTableNames(em)) { + em.createNativeQuery("TRUNCATE TABLE %s RESTART IDENTITY".formatted(tableName)).executeUpdate(); + } + em.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate(); + } + + @SuppressWarnings("unchecked") + private List findTableNames(EntityManager em) { + String tableNameSelectQuery = """ + SELECT TABLE_NAME + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = 'PUBLIC' + """; + return em.createNativeQuery(tableNameSelectQuery).getResultList(); + } +} diff --git a/backend/src/test/resources/init-test.sql b/backend/src/test/resources/init-test.sql index 90b21797b..a583b8f4b 100644 --- a/backend/src/test/resources/init-test.sql +++ b/backend/src/test/resources/init-test.sql @@ -1,34 +1,32 @@ -SET REFERENTIAL_INTEGRITY FALSE; - -TRUNCATE TABLE member; -TRUNCATE TABLE balance_option; -TRUNCATE TABLE balance_content; -TRUNCATE TABLE balance_vote; -TRUNCATE TABLE room; -TRUNCATE TABLE room_content; - -ALTER TABLE member ALTER COLUMN ID RESTART WITH 1; -ALTER TABLE balance_option ALTER COLUMN ID RESTART WITH 1; -ALTER TABLE balance_content ALTER COLUMN ID RESTART WITH 1; -ALTER TABLE balance_vote ALTER COLUMN ID RESTART WITH 1; -ALTER TABLE room ALTER COLUMN ID RESTART WITH 1; -ALTER TABLE room_content ALTER COLUMN ID RESTART WITH 1; - -INSERT INTO room() VALUES (); - -INSERT INTO member(nickname, room_id) -VALUES ('mohamedeu al katan', 1), ('deundeun', 1), ('rupi', 1), ('rapper lee', 1); - -INSERT INTO room_content(room_id, balance_content_id, created_at) -VALUES (1, 2, '2024-07-18 19:50:00.000'), (1, 1, '2024-07-18 20:00:00.000'); - -INSERT INTO balance_content(category, name) -VALUES ('EXAMPLE', '민초 vs 반민초'), ('EXAMPLE', '월 200 백수 vs 월 500 직장인'); - -INSERT INTO balance_option(name, balance_content_id) -VALUES ('민초', 1), ('반민초', 1), ('월 200 백수', 2), ('월 200 직장인', 2); - -INSERT INTO balance_vote(balance_option_id, member_id) -VALUES (4, 1), (4, 2), (4, 3), (4, 4), (1, 1), (1, 2), (1, 3), (2, 4); - -SET REFERENTIAL_INTEGRITY TRUE; +INSERT INTO room () +VALUES (); + +INSERT INTO member (nickname, room_id) +VALUES ('mohamedeu al katan', 1), + ('deundeun', 1), + ('rupi', 1), + ('rapper lee', 1); + +INSERT INTO balance_content (category, name) +VALUES ('EXAMPLE', '민초 vs 반민초'), + ('EXAMPLE', '월 200 백수 vs 월 500 직장인'); + +INSERT INTO room_content (room_id, balance_content_id, created_at) +VALUES (1, 2, '2024-07-18 19:50:00.000'), + (1, 1, '2024-07-18 20:00:00.000'); + +INSERT INTO balance_option (name, balance_content_id) +VALUES ('민초', 1), + ('반민초', 1), + ('월 200 백수', 2), + ('월 200 직장인', 2); + +INSERT INTO balance_vote (balance_option_id, member_id) +VALUES (4, 1), + (4, 2), + (4, 3), + (4, 4), + (1, 1), + (1, 2), + (1, 3), + (2, 4);