简体   繁体   English

JUnit 5 和 inheritance 具有不同的扩展,用于 Spring 引导集成测试与 TestContainers

[英]JUnit 5 and inheritance with different extensions for Spring Boot Integration test with TestContainers

Intro: Our product needs to have integrations tests for 3 different databases:简介:我们的产品需要对 3 个不同的数据库进行集成测试:

  1. Oracle Oracle
  2. Postgres Postgres
  3. MSSQL微软SQL

We are using Spring Boot as our framework and TestContainers to start up the databases mentioned above.我们使用 Spring Boot 作为我们的框架和 TestContainers 来启动上述数据库。

The problem: We need to run the same tests for each container (database).问题:我们需要为每个容器(数据库)运行相同的测试。

After a lot of digging on the net the only way that I could think of was using a BaseClass where we write all the test cases and for each container, we create a class that inherits from the BaseClass and we override the method and annotate it with @Test.在网上大量挖掘之后,我能想到的唯一方法是使用 BaseClass,我们在其中编写所有测试用例,并为每个容器创建一个继承自 BaseClass 的 class 并覆盖该方法并用@测试。

Below in the code, you will a single JUnit5 extension for Postgres that starts a TestContainer, base test class, and a test class that gets extended from the Postgres extension, starts a Spring Application context, and runs the tests from the base class. Below in the code, you will a single JUnit5 extension for Postgres that starts a TestContainer, base test class, and a test class that gets extended from the Postgres extension, starts a Spring Application context, and runs the tests from the base class.

The code:编码:

import com.company.itest.AutoConfig;
import com.company.itest.BaseIntegrationTest;
import com.company.itest.db.mssql.MSSqlTest;
import com.company.itest.db.oracle.OracleTest;
import com.company.itest.db.postgres.PostgresTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

public class TestTheTest extends BaseIntegrationTest {
  public void contextLoads() {
    Assertions.assertEquals(1, 1);
  }
  public void contextLoads2() {
    Assertions.assertNotEquals(1, 2);
  }
}


@SpringBootTest(
    classes = AutoConfig.class,
    webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@PostgresTest
class TestPostgres extends TestTheTest {
  @Test
  public void contextLoads() {
    super.contextLoads();
  }
  @Test
  public void contextLoads2() {
    super.contextLoads2();
  }
}


import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.PostgreSQLContainer;

public class PostgresqlTestContainersExtension implements BeforeAllCallback, AfterAllCallback {

  private final Logger log = LoggerFactory.getLogger(PostgresqlTestContainersExtension.class);

  private PostgreSQLContainer<?> postgres;

  @Override
  public void beforeAll(ExtensionContext context) {
    log.info("Setting up postgres container");
    postgres = new PostgreSQLContainer<>("postgres:13").withReuse(true);

    postgres.start();
    System.setProperty("spring.datasource.url", postgres.getJdbcUrl());
    System.setProperty("spring.datasource.username", postgres.getUsername());
    System.setProperty("spring.datasource.password", postgres.getPassword());
  }

  @Override
  public void afterAll(ExtensionContext context) {
    postgres.stop();
  }
}



package com.company.itest.db.postgres;

import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@ExtendWith(SpringExtension.class)
@ExtendWith({PostgresqlTestContainersExtension.class})
@Testcontainers
public @interface PostgresTest {}

The question:问题:

How can I create a single JUnit test class and then rerun it with a different JUnit5 extension without doing this polymorphism?如何创建单个 JUnit 测试 class 然后使用不同的 JUnit5 扩展重新运行它而不执行此多态性?

If you are using maven, you could try to have a different profile per db如果您使用的是 maven,您可以尝试为每个 db 设置不同的配置文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 JUnit、Spring Boot 和 TestContainers 防止集成测试中的数据冲突? - How do I prevent data conflicts in integration test using JUnit, Spring Boot and TestContainers? 如何在 spring 启动集成测试期间正确连接到测试容器 redis? - How to connect to testcontainers redis correctly during spring boot integration test? Spring 使用 testcontainers 启动测试 postgresql - Spring boot test using testcontainers postgresql 带有不同数据库环境的Spring Boot集成测试 - Spring boot Integration test with different database environmets 使用带有 JUnit5 扩展的测试容器 - Using Testcontainers with JUnit5 extensions Spring @DataJpaTest 与 @TestContainers 在 JUnit 5 - Spring @DataJpaTest with @TestContainers in JUnit 5 Spring boot、ElasticSearch 和 TestContainers 集成测试。 拒绝连接 - Spring boot, ElasticSearch and TestContainers integration tests. Connection refused 使用 Testcontainers/Localstack 和 Spring 进行集成测试 Boot:为临时文件设置目录 - Integration testing with Testcontainers/Localstack and Spring Boot: Set directory for temporary files Spring 启动+集成测试 - Spring Boot + Integration Test Spring 数据 Elasticsearch 在SpringBoot中使用Testcontainers进行集成测试 - Spring Data Elasticsearch integration test using Testcontainers in SpringBoot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM