Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] test: 테스트 환경을 MySQL로 통일하기 위해 TestContainers 도입 #771

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 0 additions & 14 deletions backend/src/test/java/reviewme/config/TestConfig.java

This file was deleted.

6 changes: 3 additions & 3 deletions backend/src/test/java/reviewme/support/DatabaseCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
3 changes: 1 addition & 2 deletions backend/src/test/java/reviewme/support/ServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
36 changes: 36 additions & 0 deletions backend/src/test/java/reviewme/support/TestDatabaseConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 0 additions & 4 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
spring:
datasource:
url: jdbc:h2:mem:test
username: sa
password:
h2:
console:
enabled: true
Expand Down