简体   繁体   English

在没有测试容器启动的情况下运行 SpringBootTest 上下文

[英]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 种类型的测试:

  1. Unit tests with no Spring context没有 Spring 上下文的单元测试
  2. Unit tests with a Spring context (including lots of mocking but still autowiring/constructor injection support)具有 Spring 上下文的单元测试(包括许多 mocking 但仍然支持自动装配/构造函数注入)
  3. Integration tests with a Spring context that spin up testcontainers and allow me to test DB interactions (and potentially end to end tests to come)使用 Spring 上下文进行集成测试,该上下文启动测试容器并允许我测试数据库交互(并可能进行端到端测试)

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.

相关问题 如何通过测试容器启动 Informix? - How to launch Informix via testcontainers? 使用测试容器作为 Dockerfile 的一部分运行测试 - Running test with testcontainers as part of a Dockerfile 带@Sql的@SpringBootTest:脚本执行和上下文初始化的顺序 - @SpringBootTest with @Sql: order of script execution and context initialization 如何模拟@springboottest 的应用程序配置或上下文? - How to mock application configuration or context for @springboottest? 没有为集成测试运行 testcontainers 的 Localstack 模块 - Localstack module of testcontainers is not running for integration tests @SpringBootTest 没有声明每个需要的类 - @SpringBootTest without declaring every needed class 我想在不使用 @SpringBootTest 的情况下测试 controller - I want to test a controller without using @SpringBootTest 带有 testcontainers 的 Spring Boot - 如何在上下文重新加载时防止数据库初始化 - Spring boot with testcontainers - how to prevent DB initialization on context reload 接受来自 SpringBootTest 上下文的外部 REST 调用并验证调用 - Accept external REST calls from SpringBootTest context and verify the call SpringBootTest-如何断言上下文未加载并在测试级别更改属性? - SpringBootTest - how to assert context not loads and change properties at test level?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM