如何重用 TestContainer ? (6月4日)
How to reuse TestContainer ? (Junit 4)
大家好 :) 我有3个问题:
- 如何在 Junit 4 中重用 TestContainer?
- 我如何验证测试期间使用的容器数量?
- 默认为每个
@Test
或整个 class 启动一个新容器?
提前感谢您的回答
PostgresTestContainer.java
@ContextConfiguration(initializers = PostgresTestContainer.Initializer.class)
public abstract class PostgresTestContainer {
@ClassRule
public static PostgreSQLContainer postgresContainer = new PostgreSQLContainer(TCConfig.POSTGRESQL_VERSION.toString())
.withDatabaseName(TCConfig.TC_DBNAME)
.withUsername(TCConfig.TC_USERNAME)
.withPassword(TCConfig.TC_PASSWORD);
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static String stringConnection = postgresContainer.getJdbcUrl();
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues values = TestPropertyValues.of(
"spring.datasource.url=" + stringConnection,
"spring.datasource.username=" + TCConfig.TC_USERNAME,
"spring.datasource.password=" + TCConfig.TC_PASSWORD
);
values.applyTo(applicationContext);
}
}
}
PostgreSQL12Test.java
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class PostgreSQL12_Test extends PostgresTestContainer {
@Autowired
private MemberService memberService;
@Autowired
private Flyway flyway;
@Before
public void initialize() {
flyway.migrate();
}
@Test
public void shoudRunPostgreSQLContainer() throws Exception {
Connection connection = DriverManager.getConnection(postgresContainer.getJdbcUrl(), postgresContainer.getUsername(), postgresContainer.getPassword());
ResultSet resultSet = connection.createStatement().executeQuery("SELECT 666");
resultSet.next();
int result = resultSet.getInt(1);
assertThat(result).isEqualByComparingTo(666);
}
}
版本
TestContainers - Postgresql : 1.13.0
Spring Boot : 2.0.0 ( Junit 4 )
Docker : 19.03.11
Os : 20.04.1 LTS (Focal Fossa)
如何在 Junit 4 中重用 TestContainer?
它应该已经按照您编写测试的方式工作了。你有
用@ClassRule 注释的容器,因此它应该只加载一次。
如何验证测试期间使用的容器数量?
在您的测试方法中放置一个断点,并在终端中放置 运行 docker ps
。
默认为每个@Test 或整个 class?
启动一个新容器
使用@ClassRule 应该为class 创建它。你可以删除
该注解和容器的生命周期将被管理
通过 java 本身(如果该字段是静态的并且对于每个测试方法
如果不是)
要为所有测试重用 Container class 只需使用 static
,而不使用 @ClassRule
或 @Rule
。
public class PostgresTestContainer {
public static final PostgreSQLContainer POSTGRESQL_CONTAINER = new PostgreSQLContainer<>(DockerImageName.parse("postgres:9.6.12"))
.withDatabaseName("db_name")
.withUsername("db_user")
.withPassword("db_pass");
static {
POSTGRE_SQL_CONTAINER.start();
}
}
大家好 :) 我有3个问题:
- 如何在 Junit 4 中重用 TestContainer?
- 我如何验证测试期间使用的容器数量?
- 默认为每个
@Test
或整个 class 启动一个新容器?
提前感谢您的回答
PostgresTestContainer.java
@ContextConfiguration(initializers = PostgresTestContainer.Initializer.class)
public abstract class PostgresTestContainer {
@ClassRule
public static PostgreSQLContainer postgresContainer = new PostgreSQLContainer(TCConfig.POSTGRESQL_VERSION.toString())
.withDatabaseName(TCConfig.TC_DBNAME)
.withUsername(TCConfig.TC_USERNAME)
.withPassword(TCConfig.TC_PASSWORD);
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static String stringConnection = postgresContainer.getJdbcUrl();
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues values = TestPropertyValues.of(
"spring.datasource.url=" + stringConnection,
"spring.datasource.username=" + TCConfig.TC_USERNAME,
"spring.datasource.password=" + TCConfig.TC_PASSWORD
);
values.applyTo(applicationContext);
}
}
}
PostgreSQL12Test.java
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class PostgreSQL12_Test extends PostgresTestContainer {
@Autowired
private MemberService memberService;
@Autowired
private Flyway flyway;
@Before
public void initialize() {
flyway.migrate();
}
@Test
public void shoudRunPostgreSQLContainer() throws Exception {
Connection connection = DriverManager.getConnection(postgresContainer.getJdbcUrl(), postgresContainer.getUsername(), postgresContainer.getPassword());
ResultSet resultSet = connection.createStatement().executeQuery("SELECT 666");
resultSet.next();
int result = resultSet.getInt(1);
assertThat(result).isEqualByComparingTo(666);
}
}
版本
TestContainers - Postgresql : 1.13.0
Spring Boot : 2.0.0 ( Junit 4 )
Docker : 19.03.11
Os : 20.04.1 LTS (Focal Fossa)
如何在 Junit 4 中重用 TestContainer?
它应该已经按照您编写测试的方式工作了。你有 用@ClassRule 注释的容器,因此它应该只加载一次。
如何验证测试期间使用的容器数量?
在您的测试方法中放置一个断点,并在终端中放置 运行
docker ps
。默认为每个@Test 或整个 class?
启动一个新容器使用@ClassRule 应该为class 创建它。你可以删除 该注解和容器的生命周期将被管理 通过 java 本身(如果该字段是静态的并且对于每个测试方法 如果不是)
要为所有测试重用 Container class 只需使用 static
,而不使用 @ClassRule
或 @Rule
。
public class PostgresTestContainer {
public static final PostgreSQLContainer POSTGRESQL_CONTAINER = new PostgreSQLContainer<>(DockerImageName.parse("postgres:9.6.12"))
.withDatabaseName("db_name")
.withUsername("db_user")
.withPassword("db_pass");
static {
POSTGRE_SQL_CONTAINER.start();
}
}