我正在使用TestContainers和Spring Boot运行存储库的单元测试,例如:
@Testcontainers
@ExtendWith(SpringExtension.class)
@ActiveProfiles("itest")
@SpringBootTest(classes = RouteTestingCheapRouteDetector.class)
@ContextConfiguration(initializers = AlwaysFailingRouteRepositoryShould.Initializer.class)
@TestExecutionListeners(listeners = DependencyInjectionTestExecutionListener.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Tag("docker")
@Tag("database")
class AlwaysFailingRouteRepositoryShould {
@SuppressWarnings("rawtypes")
@Container
private static final PostgreSQLContainer database =
new PostgreSQLContainer("postgres:9.6")
.withDatabaseName("database")
.withUsername("postgres")
.withPassword("postgres");
现在我有14个这样的测试,每次运行测试都会启动一个新的Postgres实例。是否可以在所有测试中重用同一实例?由于每次测试都会启动一个新应用程序,单例模式并没有帮助。
我还尝试了在.testcontainers.properties
和.withReuse(true)
中使用testcontainers.reuse.enable=true
,但这并没有起到帮助作用。
testcontainers.reuse.enable=true
- 也没有帮助。 - Martin Schröder@TestConfiguration
中作为@Bean
,然后在所有相关测试中导入此配置。 - Nikolai Shevchenko@ContextConfiguration(initializers
中使用,我无法让它在那里工作。 - Martin Schröder