[英]Running SpringBootTest Context Without Testcontainers Launch
I have 2 parent test classes:我有 2 个家长测试课程:
@SpringBootTest(properties = {
"spring.datasource.url=jdbc:tc:mysql:8.0.25:///my_test_db?TC_INITSCRIPT=db/init_mysql.sql",
"spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver"
})
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
public abstract class UserApplicationIntegrationTest {
}
and和
@SpringBootTest
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
public abstract class UserApplicationTest {
}
The idea is for various test classes to extend these classes.这个想法是让各种测试类扩展这些类。 The ones which require a mocked MySQL DB will extend UserApplicationIntegrationTest.
需要模拟 MySQL DB 的数据库将扩展 UserApplicationIntegrationTest。 Ones which don't need a DB connection but that do require a Spring context will extend UserApplicationTest.
不需要数据库连接但需要 Spring 上下文的那些将扩展 UserApplicationTest。
In the absence of UserApplicationIntegrationTest, all the test classes extending UserApplicationTest work well, including using the Mockito framework.在没有 UserApplicationIntegrationTest 的情况下,所有扩展 UserApplicationTest 的测试类都运行良好,包括使用 Mockito 框架。 Unfortunately, when I introduce UserApplicationIntegrationTest and its sub-tests (which work perfectly with the dockerised db instance), these tests begin to fail as they suddenly demand a datasource.
不幸的是,当我介绍 UserApplicationIntegrationTest 及其子测试(与 dockerised db 实例完美配合)时,这些测试开始失败,因为它们突然需要数据源。
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
If I try excluding datasource auto-configuration either in app properties or in annotations of the parent class, the testcontainers tests (those extending UserApplicationIntegrationTest) start failing because of a problem with the Spring context and not being able to autowire beans any longer in those tests.如果我尝试在应用程序属性或父 class 的注释中排除数据源自动配置,则由于 Spring 上下文存在问题,testcontainers 测试(那些扩展 UserApplicationIntegrationTest)开始失败,并且无法在这些测试中再自动装配 bean .
Before I know it, I'm down a rabbit hole of attempting messy exclusions/additions that I've been down before in previous projects and it only leads to problems further down the line.在我知道之前,我已经陷入了一个尝试混乱的排除/添加的兔子洞,这是我以前在以前的项目中遇到过的,它只会导致进一步的问题。
Essentially I want 3 types of tests coexisting in my project:本质上,我希望在我的项目中共存 3 种类型的测试:
The original reason that I wanted to avoid launching testcontainers for all Spring context tests (which would 'work' perfectly well and only include 1 docker delay in the build process) was because it was irritating me to have to wait for the mysql connection to the dockerised instance every time I ran individual Spring context tests locally during development.我想避免为所有 Spring 上下文测试启动测试容器的最初原因(这将“工作”得很好,并且只包括构建过程中的 1 docker 延迟)是因为它让我不得不等待 Z825B6053C41A2130AFD6FC3B158BDA4E6Z 延迟连接到 Z825B6053C41A2130AFD6FC3B158BDA4E6Z 让我很恼火。每次我在开发过程中在本地运行单独的 Spring 上下文测试时,都会对实例进行 dockerised。
Is there a tidy way to achieve this or an altogether better way of navigating the requirement?有没有一种简洁的方法来实现这一目标,或者有一种更好的方法来满足需求?
Thanks in advance.提前致谢。
Hopefully I understand you right, what I did was implementing an abstract TestContainer test class:希望我理解正确,我所做的是实现一个抽象的 TestContainer 测试 class:
package de.dwosch.it;
@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()
);
}
}
}
Then I just extend my test classes by this abstract class which will fire up a test container and the whole spring context for better separation然后我只是通过这个抽象的 class 扩展我的测试类,它将启动一个测试容器和整个 spring 上下文以便更好地分离
class MyAwesomeControllerIT extends AbstractPostgreSQLTestContainerIT { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.