JUnit5 - 如何将输入集合传递给 ParameterizedTest

问题描述

我正在尝试将 ParameterizedTest 从 JUnit4 转换为 junit5(遗憾的是我不是特别擅长测试)。

在 JUnit4 中,我有以下类:

@RunWith(Parameterized.class)
public class AssertionTestCase {
   private final TestInput testInput;

   public AssertionTestCase(TestInput testInput) {
       this.testInput = testInput;
   }

   @Parameterized.Parameters
   public static Collection<Object[]> data() {
       return AssertionTestCaseDataProvider.createDataCase();
   }

   @Test(timeout = 15 * 60 * 1000L)
   public void testDailyAssertion() {
       LOG.info("Testing input {}/{}",testInput.getTestCase(),testInput.getTestName());

       //assert stuffs
   }
 }

AssertionTestCaseDataProvider 类中,我有一个生成 Object[] 集合的简单方法

class AssertionTestCaseDataProvider {

   static Collection<Object[]> createDataCase() {
       final List<TestInput> testInputs = new ArrayList<>();

       //create and populate testInputs

       return testInputs.stream()
               .map(testInput -> new Object[]{testInput})
               .collect(Collectors.toList());
   }
}

我一直在尝试使用 junit5 翻译它并获得了这个:

class AssertionTestCase {

   private final TestInput testInput;

   public AssertionTestCase(TestInput testInput) {
       this.testInput = testInput;
   }

   public static Collection<Object[]> data() {
       return AssertionTestCaseDataProvider.createDataCase();
   }

   @ParameterizedTest
   @MethodSource("data")
   void testDailyAssertion() {
       LOG.info("Testing input {}/{}",testInput.getTestName());

       // assert stuffs
   } 

}

我没有对 AssertionTestCaseDataProvider 类应用任何更改。 不过,我收到以下错误

No ParameterResolver registered for parameter [com.xxx.xx.xxx.xxx.testinput.TestInput arg0] in constructor [public `com.xxx.xxx.xxx.xxx.xxx.AssertionTestCase(com.xxx.xxx.xxx.xxx.testinput.TestInput)]. org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [com.xxx.xx.xxx.xxx.testinput.TestInput arg0] in constructor [public com.xxx.xxx.xxx.xxx.xxx.AssertionTestCase(com.xxx.xxx.xxx.xxx.testinput.TestInput)].`

我知道在初始化测试的输入集合时我可能没有正确应用 junit5。我是否缺少一些注释?

我还尝试使用 @ArgumentSource 代替 @MethodSource 并为 Argument 实现 AssertionTestCaseDataProvider,但结果相同。

解决方法

它在 Junit5 中的工作方式有点不同。 测试方法应该有参数,提供者方法应该返回一个 Stream

static Stream<Arguments> data(){
    return Stream.of(
       Arguments.of("a",1),Arguments.of("d",2)
    );
}

@ParameterizedTest
@MethodSource("data")
void testDailyAssertion(String a,int b) {
    Assertions.assertAll(
            () -> Assertions.assertEquals("a",a),() -> Assertions.assertEquals(1,b)
    );
}

在你的情况下,你可以只返回一个 Stream:

static Stream<TestInput> createDataCase() {
   final List<TestInput> testInputs = new ArrayList<>();

   //create and populate testInputs

   return testInputs.stream();
}

然后在您的测试方法中:

@ParameterizedTest
@MethodSource("createDataCase")
void testDailyAssertion(TestInput testInput) {
    {your assertions}
} 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...