From 228515abc9a02e7a8745681b6a9c0264112194a1 Mon Sep 17 00:00:00 2001 From: donghoony Date: Sat, 5 Oct 2024 21:13:37 +0900 Subject: [PATCH 1/3] =?UTF-8?q?build:=20testcontainers=20=EC=9D=98?= =?UTF-8?q?=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/build.gradle b/backend/build.gradle index bd3a976cf..47f1539a4 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -49,6 +49,9 @@ dependencies { testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc:3.0.1' testImplementation 'io.rest-assured:spring-mock-mvc:5.4.0' testImplementation 'io.rest-assured:rest-assured:5.4.0' + + // TestContainers + testImplementation 'org.testcontainers:mysql:1.20.2' } ext { From 26f2aa81d6c87a8cab2e245a0362c8a8a678412d Mon Sep 17 00:00:00 2001 From: donghoony Date: Sat, 5 Oct 2024 21:14:41 +0900 Subject: [PATCH 2/3] =?UTF-8?q?test:=20`DataSource`=20=EB=B9=88=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D,=20TestConfig=20=EB=AA=85=ED=99=95=ED=95=98?= =?UTF-8?q?=EA=B2=8C=20=EC=9C=84=EC=B9=98=20=EC=88=98=EC=A0=95=20=EB=B0=8F?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/reviewme/config/TestConfig.java | 14 -------- .../java/reviewme/support/ServiceTest.java | 3 +- .../reviewme/support/TestDatabaseConfig.java | 36 +++++++++++++++++++ backend/src/test/resources/application.yml | 4 --- 4 files changed, 37 insertions(+), 20 deletions(-) delete mode 100644 backend/src/test/java/reviewme/config/TestConfig.java create mode 100644 backend/src/test/java/reviewme/support/TestDatabaseConfig.java diff --git a/backend/src/test/java/reviewme/config/TestConfig.java b/backend/src/test/java/reviewme/config/TestConfig.java deleted file mode 100644 index f339dd641..000000000 --- a/backend/src/test/java/reviewme/config/TestConfig.java +++ /dev/null @@ -1,14 +0,0 @@ -package reviewme.config; - -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.context.annotation.Bean; -import reviewme.support.DatabaseCleaner; - -@TestConfiguration -public class TestConfig { - - @Bean - public DatabaseCleaner databaseCleaner() { - return new DatabaseCleaner(); - } -} diff --git a/backend/src/test/java/reviewme/support/ServiceTest.java b/backend/src/test/java/reviewme/support/ServiceTest.java index 34ae4b4fd..85837b742 100644 --- a/backend/src/test/java/reviewme/support/ServiceTest.java +++ b/backend/src/test/java/reviewme/support/ServiceTest.java @@ -7,11 +7,10 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import reviewme.config.TestConfig; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) -@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = TestConfig.class) +@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = TestDatabaseConfig.class) @ExtendWith(DatabaseCleanerExtension.class) public @interface ServiceTest { } diff --git a/backend/src/test/java/reviewme/support/TestDatabaseConfig.java b/backend/src/test/java/reviewme/support/TestDatabaseConfig.java new file mode 100644 index 000000000..619241355 --- /dev/null +++ b/backend/src/test/java/reviewme/support/TestDatabaseConfig.java @@ -0,0 +1,36 @@ +package reviewme.support; + +import javax.sql.DataSource; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; +import org.testcontainers.containers.JdbcDatabaseContainer; +import org.testcontainers.containers.MySQLContainer; + +@TestConfiguration +public class TestDatabaseConfig { + + private static final String IMAGE_NAME = "mysql:8.0.39"; + private static final JdbcDatabaseContainer container = new MySQLContainer<>(IMAGE_NAME); + + static { + container.start(); + } + + @Bean + @Primary + public static DataSource getDataSource() { + return DataSourceBuilder.create() + .url(container.getJdbcUrl()) + .username(container.getUsername()) + .password(container.getPassword()) + .driverClassName(container.getDriverClassName()) + .build(); + } + + @Bean + public DatabaseCleaner databaseCleaner() { + return new DatabaseCleaner(); + } +} diff --git a/backend/src/test/resources/application.yml b/backend/src/test/resources/application.yml index 0c5a19c1e..a717f39bd 100644 --- a/backend/src/test/resources/application.yml +++ b/backend/src/test/resources/application.yml @@ -1,8 +1,4 @@ spring: - datasource: - url: jdbc:h2:mem:test - username: sa - password: h2: console: enabled: true From 6a4813b97042ba54ec314c76887f845758cc65d9 Mon Sep 17 00:00:00 2001 From: donghoony Date: Sat, 5 Oct 2024 21:14:58 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20MySQL=20=ED=98=95=EC=8B=9D=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20Alter=20table=20=EB=B0=8F=20=EC=99=B8=EB=9E=98?= =?UTF-8?q?=ED=82=A4=20=EB=AC=B4=EA=B2=B0=EC=84=B1=20=ED=95=B4=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/test/java/reviewme/support/DatabaseCleaner.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/test/java/reviewme/support/DatabaseCleaner.java b/backend/src/test/java/reviewme/support/DatabaseCleaner.java index 50801b02f..5ecbc500d 100644 --- a/backend/src/test/java/reviewme/support/DatabaseCleaner.java +++ b/backend/src/test/java/reviewme/support/DatabaseCleaner.java @@ -13,7 +13,7 @@ public class DatabaseCleaner { private static final String TRUNCATE_FORMAT = "TRUNCATE TABLE %s"; - private static final String ALTER_FORMAT = "ALTER TABLE %s ALTER COLUMN ID RESTART WITH 1"; + private static final String ALTER_FORMAT = "ALTER TABLE %s AUTO_INCREMENT = 1"; @PersistenceContext private EntityManager entityManager; @@ -32,11 +32,11 @@ public void afterPropertiesSet() { @Transactional public void execute() { entityManager.flush(); - entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate(); + entityManager.createNativeQuery("SET FOREIGN_KEY_CHECKS = 0").executeUpdate(); for (String tableName : tableNames) { entityManager.createNativeQuery(TRUNCATE_FORMAT.formatted(tableName)).executeUpdate(); entityManager.createNativeQuery(ALTER_FORMAT.formatted(tableName)).executeUpdate(); } - entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate(); + entityManager.createNativeQuery("SET FOREIGN_KEY_CHECKS = 1").executeUpdate(); } }