如何使用 Junit 正确实施 Flyway?
我将 Flyway 添加到我的项目中,并将 V1__init.sql (例如)放入该**main**/resources/db/migration目录中。只要我调试我的代码(无需测试即可启动)它就可以工作。
我也必须将*.sql文件复制到**test**/resources/db/migration目录中吗?
我还希望针对另一个数据库(而不是测试数据库)进行测试。我是否正确,我必须在放置构建数据库凭据的文件夹application.properties下添加另一个文件(测试运行的位置?)。test
如果有人可以帮助我了解如何以正确的方式配置它,我会非常高兴。
小智 6
您可以将迁移文件保留在 src 下,不需要将它们复制到测试文件夹中。它们将在运行@SpringBootTest. 这也确保您将所有production迁移用于测试
此外,您不一定需要单独的属性文件来进行测试。但你可以拥有一个。
以下是 IntegrationTesting 的示例TestContainers,它使用application.propertiesFlyway 迁移,以便测试的行为与您通常运行应用程序的行为一样。
这是一个抽象类,它确保测试在整个 Spring 上下文中运行,因此Flyway也涉及到。在初始化程序中,数据源配置属性被 的数据库中的属性覆盖TestContainers。这样做你可以直接使用真实的application.properties并模拟一点真实的;))
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ContextConfiguration(initializers = AbstractPostgreSQLTestContainerIT.Initializer.class)
@Testcontainers
public abstract class AbstractPostgreSQLTestContainerIT {
private static final String POSTGRES_VERSION = "postgres:11.1";
public static PostgreSQLContainer database;
static {
database = new PostgreSQLContainer(POSTGRES_VERSION);
database.start();
}
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
configurableApplicationContext,
"spring.datasource.url=" + database.getJdbcUrl(),
"spring.datasource.username=" + database.getUsername(),
"spring.datasource.password=" + database.getPassword()
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在您可以定义几个测试类,如下所示:
class MyIntegrationTest extends AbstractPostgreSQLTestContainerIT { }
Run Code Online (Sandbox Code Playgroud)
当在此类中运行测试时,SpringBoot 应用程序将启动并使用TestContainers数据库。
为了我的目的,我还实现了一个简单的注释:
void updateFooByExternalIdentifier_DTOProvided_ShouldReturnUpdatedFoo() {}
Run Code Online (Sandbox Code Playgroud)
代码
/**
* Annotation which allows to provide SQL Scripts for a certain IT test.
* The transactional ensures that data is cleaned up after test.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional
@Test
@Sql
public @interface TransactionalSQLTest {
@AliasFor(attribute = "value", annotation = Sql.class)
String[] value() default {};
@AliasFor(attribute = "executionPhase", annotation = Sql.class)
Sql.ExecutionPhase executionPhase() default Sql.ExecutionPhase.BEFORE_TEST_METHOD;
}
Run Code Online (Sandbox Code Playgroud)
使用注释,您可以为 SQL 提供测试样本数据等。
pom.xml
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这应该是MySql的依赖
<!-- https://mvnrepository.com/artifact/org.testcontainers/mysql -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.15.3</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13710 次 |
| 最近记录: |