public class SchemaSqlLoader {
public static void executeSchemaSql(Connection connection, String schemaPath) throws Exception {
try (InputStream input = SchemaSqlLoader.class.getClassLoader().getResourceAsStream(schemaPath)) {
if (input == null) throw new IllegalArgumentException("schema.sql not found");
String sql = new String(input.readAllBytes(), StandardCharsets.UTF_8);
String[] statements = sql.split(";");
try (Statement stmt = connection.createStatement()) {
for (String s : statements) {
String trimmed = s.trim();
if (!trimmed.isEmpty()) {
stmt.execute(trimmed);
}
}
}
}
}
}
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
import java.sql.Connection;
@SpringBootTest
public class DbUnitTest {
@Autowired
private DataSource dataSource;
@BeforeEach
void setUp() throws Exception {
try (Connection conn = dataSource.getConnection()) {
// schema.sql 실행
SchemaSqlLoader.executeSchemaSql(conn, "schema.sql");
// DBUnit 초기화
IDatabaseConnection dbUnitConn = new DatabaseConnection(conn);
// 여기에서 원하는 DBUnit 데이터셋 적용 가능 (예: Java 코드로 수동 삽입)
}
}
@Test
void someTest() {
// 테스트 실행
}
}