簡體   English   中英

JUnit 5 與 Spring 啟動 - 如何使用不同的配置文件重復測試?

[英]JUnit 5 with Spring boot - how repeat test with different profiles?

這是 JUnit 5 數據驅動測試的經典示例。

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
class ScrathTest {
  @Autowired
  private MyBean myBean;

  @ParameterizedTest
  @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) // six numbers
  void isOdd_ShouldReturnTrueForOddNumbers(int number) {
    myBean.doSomeThing(number)
  }
}

但是,如果我需要的不是 integer 數組而是配置文件數組,該怎么辦? 我的意思是我創建了一個測試和 3 個測試配置文件,然后在每個創新中使用不同的配置文件重復這個測試 3 次。 可能嗎?

筆記

@ActiveProfile注釋不是一個解決方案,因為它只是激活列出的配置文件而無需重復測試和重新創建上下文。

我不認為JUnit5提供了類似的功能。 但是,您可以創建一個抽象 class 並且派生的 class 將指定活動配置文件。

class abstract AbstactScrathTest {

  @Autowired
  protected MyBean myBean;

  @ParameterizedTest
  @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) // six numbers
  void isOdd_ShouldReturnTrueForOddNumbers(int number) {
    myBean.doSomeThing(number)
  }

}

@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test1")
class ScrathTestWithTestProfile1 extends AbstractScrathTest{
}


@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test2")
class ScrathTestWithTestProfile2 extends AbstractScrathTest{
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM