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 { 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/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(); } } 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