-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: TestContainer 적용 * test: TestContainer MySQL 버전 5.7로 변경 * test: RestAssured port 선언조건 추가 (cherry picked from commit e491214)
- Loading branch information
1 parent
3c61882
commit 5d8db6b
Showing
5 changed files
with
131 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
spring: | ||
jpa: | ||
hibernate: | ||
ddl-auto: create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package in.koreatech.koin; | ||
|
||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
|
||
import in.koreatech.koin.support.DBInitializer; | ||
import io.restassured.RestAssured; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.containers.MySQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
|
||
@SpringBootTest(webEnvironment = RANDOM_PORT) | ||
@Import(DBInitializer.class) | ||
@ActiveProfiles("test") | ||
public abstract class AcceptanceTest { | ||
|
||
private static final String ROOT = "test"; | ||
private static final String ROOT_PASSWORD = "1234"; | ||
|
||
@LocalServerPort | ||
protected int port; | ||
|
||
@Autowired | ||
private DBInitializer dataInitializer; | ||
|
||
@Container | ||
protected static MySQLContainer container; | ||
|
||
@DynamicPropertySource | ||
private static void configureProperties(final DynamicPropertyRegistry registry) { | ||
registry.add("spring.datasource.url", container::getJdbcUrl); | ||
registry.add("spring.datasource.username", () -> ROOT); | ||
registry.add("spring.datasource.password", () -> ROOT_PASSWORD); | ||
} | ||
|
||
static { | ||
container = (MySQLContainer) new MySQLContainer("mysql:5.7.34") | ||
.withDatabaseName("test") | ||
.withUsername(ROOT) | ||
.withPassword(ROOT_PASSWORD) | ||
.withCommand("--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"); | ||
container.start(); | ||
} | ||
|
||
@BeforeEach | ||
void delete() { | ||
if (RestAssured.port == RestAssured.UNDEFINED_PORT) { | ||
RestAssured.port = port; | ||
} | ||
dataInitializer.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/in/koreatech/koin/support/DBInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package in.koreatech.koin.support; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.sql.DataSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.TestComponent; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@TestComponent | ||
public class DBInitializer { | ||
|
||
private static final int OFF = 0; | ||
private static final int ON = 1; | ||
private static final int COLUMN_INDEX = 1; | ||
|
||
private final List<String> tableNames = new ArrayList<>(); | ||
|
||
@Autowired | ||
private DataSource dataSource; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
private void findDatabaseTableNames() { | ||
try (final Statement statement = dataSource.getConnection().createStatement()) { | ||
ResultSet resultSet = statement.executeQuery("SHOW TABLES"); | ||
while (resultSet.next()) { | ||
final String tableName = resultSet.getString(COLUMN_INDEX); | ||
tableNames.add(tableName); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void truncate() { | ||
setForeignKeyCheck(OFF); | ||
for (String tableName : tableNames) { | ||
entityManager.createNativeQuery(String.format("TRUNCATE TABLE %s", tableName)).executeUpdate(); | ||
} | ||
setForeignKeyCheck(ON); | ||
} | ||
|
||
private void setForeignKeyCheck(int mode) { | ||
entityManager.createNativeQuery(String.format("SET FOREIGN_KEY_CHECKS = %d", mode)).executeUpdate(); | ||
} | ||
|
||
@Transactional | ||
public void clear() { | ||
if (tableNames.isEmpty()) { | ||
findDatabaseTableNames(); | ||
} | ||
entityManager.clear(); | ||
truncate(); | ||
} | ||
} |