Spring Boot TestContainers 映射端口只能在容器启动后获取
Spring Boot TestContainers Mapped port can only be obtained after the container is started
我正在尝试使用 TestContainers 库将自动化测试添加到我的 Spring 启动项目
这是我的测试 class 来测试我的 jpa 存储库:
package com.ubm.mfi.repo;
import com.ubm.mfi.domain.MasterFileIndexRow;
import org.junit.ClassRule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
@ContextConfiguration(initializers = { MasterFileIndexRowRepoTest.Initializer.class })
public class MasterFileIndexRowRepoTest {
@ClassRule
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:latest");
@Autowired
private MasterFileIndexRowRepo masterFileIndexRowRepo;
// write test cases here
@Test
public void whenFindAllRows_thenSizeIsGreaterThanZero() {
// when
List<MasterFileIndexRow> rows = masterFileIndexRowRepo.findAll();
// then
assertThat(rows.size())
.isGreaterThan(0);
}
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues
.of("spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}
这是我的 build.gradle
中的依赖项
testCompile "org.testcontainers:testcontainers:1.14.1"
testCompile "org.testcontainers:postgresql:1.14.1"
当 运行 测试时我得到这个错误: Caused by: java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
据我所见,容器应该在开始测试时启动,有人知道我缺少什么吗?
您正在尝试将 PostgresSQLContainer
用作 JUnit ClassRule
,但您对 @ExtendWith
的使用似乎表明您使用的是不支持 JUnit 4 规则的 JUnit 5/Jupiter。
改为使用 JUnit 5 集成测试容器:https://www.testcontainers.org/test_framework_integration/junit_5/
我正在尝试使用 TestContainers 库将自动化测试添加到我的 Spring 启动项目
这是我的测试 class 来测试我的 jpa 存储库:
package com.ubm.mfi.repo;
import com.ubm.mfi.domain.MasterFileIndexRow;
import org.junit.ClassRule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
@ContextConfiguration(initializers = { MasterFileIndexRowRepoTest.Initializer.class })
public class MasterFileIndexRowRepoTest {
@ClassRule
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:latest");
@Autowired
private MasterFileIndexRowRepo masterFileIndexRowRepo;
// write test cases here
@Test
public void whenFindAllRows_thenSizeIsGreaterThanZero() {
// when
List<MasterFileIndexRow> rows = masterFileIndexRowRepo.findAll();
// then
assertThat(rows.size())
.isGreaterThan(0);
}
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues
.of("spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword())
.applyTo(configurableApplicationContext.getEnvironment());
}
}
}
这是我的 build.gradle
中的依赖项testCompile "org.testcontainers:testcontainers:1.14.1"
testCompile "org.testcontainers:postgresql:1.14.1"
当 运行 测试时我得到这个错误: Caused by: java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
据我所见,容器应该在开始测试时启动,有人知道我缺少什么吗?
您正在尝试将 PostgresSQLContainer
用作 JUnit ClassRule
,但您对 @ExtendWith
的使用似乎表明您使用的是不支持 JUnit 4 规则的 JUnit 5/Jupiter。
改为使用 JUnit 5 集成测试容器:https://www.testcontainers.org/test_framework_integration/junit_5/