Spring Boot/JUnit,针对多个配置文件运行所有单元测试 [英] Spring Boot / JUnit, run all unit-tests for multiple profiles

查看:115
本文介绍了Spring Boot/JUnit,针对多个配置文件运行所有单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由几个测试组成的BaseTest类.每个测试都应针对我列出的每个配置文件执行.

I have a BaseTest class which consists of several tests. Each test shall be executed for EVERY profile I list.

我考虑过使用参数化值,例如:

I thought about using Parameterized values such as:

@RunWith(Parameterized.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// @ActiveProfiles("h2-test") // <-- how to iterate over this?
public abstract class BaseTest {

@Autowired
private TestRepository test;

// to be used with Parameterized/Spring
private TestContextManager testContextManager;

public BaseTest(String profile) {
   System.setProperty("spring.profiles.active", profile);
   // TODO what now?
}

@Parameterized.Parameters
public static Collection<Object[]> data() {
  Collection<Object[]> params = new ArrayList<>();
  params.add(new Object[] {"h2-test" });
  params.add(new Object[] {"mysql-test" });
  return params;
}

@Before 
public void setUp() throws Exception {
  this.testContextManager = new TestContextManager(getClass());
  this.testContextManager.prepareTestInstance(this);
  // maybe I can spinup Spring here with my profile?
}

@Test
public void testRepository() {
  Assert.assertTrue(test.exists("foo"))
}

我如何告诉Spring使用这些不同的配置文件运行每个测试?实际上,每个配置文件都会与不同的数据源(内存中的h2,外部mysql,外部oracle等)进行通信,因此必须重新初始化我的存储库/数据源.

How would I tell Spring to run each test with these different profiles? In fact, each profile will talk to different datasources (in-memory h2, external mysql, external oracle, ..) so my repository/datasource has to be reinitialized.

我知道我可以指定@ActiveProfiles(...),甚至可以从BaseTest扩展并覆盖ActiveProfile注释.尽管这行得通,但我只展示了我的测试套件的一部分.我的许多测试类都是从BaseTest扩展而来的,我不想为每个类创建几个不同的配置文件存根.当前有效,但难看的解决方案:

  • BaseTest(@ActiveProfiles("mysql"))
    • FooClassMySQL(来自BaseTest的注释)
      • FooClassH2(@ActiveProfiles("h2"))
      • BaseTest (@ActiveProfiles("mysql"))
        • FooClassMySQL(annotation from BaseTest)
          • FooClassH2(@ActiveProfiles("h2"))
          • BarClassH2(@ActiveProfiles("h2"))

          谢谢

          推荐答案

          如果使用Maven,则实际上可以从命令行指定活动配置文件(或在需要时使用env变量):

          If you use Maven you can actually specify active profile from command line (or env variable if needed):

          mvn clean test -Dspring.profiles.active=h2-test
          

          在这种情况下,使用参数化测试的方法可能不起作用,因为必须在Spring启动其上下文之前指定配置文件.在这种情况下,当您运行参数化集成测试时,上下文将在测试运行程序开始运行测试之前已经启动.另外,出于其他原因发明了JUnit的参数化测试(运行具有不同数据系列的单元测试).

          The approach with parameterized test may not work in this case, because profile has to be specified before Spring boots up its context. In this case when you run parameterized integration test the context will be already booted up before test runner starts running your test. Also JUnit's parameterized tests were invented for other reasons (running unit tests with different data series).

          还有另外一件事-当您决定使用@RunWith(Parameterized.class)时,您将无法使用其他跑步者.在许多情况下(如果不是全部,如果涉及集成测试),您想要指定其他运行程序,例如SpringRunner.class-使用参数化测试,您将无法做到这一点.

          Also one more thing - when you decide to use @RunWith(Parameterized.class) you wont be able to use different runner. In many cases (if not all if it comes to integration testing) you want to specify different runner, like SpringRunner.class - with parameterized test you wont be able to do it.

          这篇关于Spring Boot/JUnit,针对多个配置文件运行所有单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆