JUnit5 - 如何将输入集合传递给 ParameterizedTest
JUnit5 - how to pass input collection to ParameterizedTest
我正在尝试将 ParameterizedTest
从 JUnit4 翻译成 JUnit5(遗憾的是我在测试方面不是特别熟练)。
在 JUnit4 中我有以下 class:
@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
class 我有一个生成集合的简单方法 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.getTestCase(), testInput.getTestName());
// assert stuffs
}
}
我没有对 AssertionTestCaseDataProvider
class 应用任何更改。
不过,我收到以下错误:
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
并为 AssertionTestCaseDataProvider
实施 Argument
,结果相同。
它在 Junit5 中的工作方式有点不同。
测试方法应该有参数,提供者方法应该 return a 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)
);
}
在你的情况下你可以 return 一个 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}
}
我正在尝试将 ParameterizedTest
从 JUnit4 翻译成 JUnit5(遗憾的是我在测试方面不是特别熟练)。
在 JUnit4 中我有以下 class:
@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
class 我有一个生成集合的简单方法 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.getTestCase(), testInput.getTestName());
// assert stuffs
}
}
我没有对 AssertionTestCaseDataProvider
class 应用任何更改。
不过,我收到以下错误:
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
并为 AssertionTestCaseDataProvider
实施 Argument
,结果相同。
它在 Junit5 中的工作方式有点不同。
测试方法应该有参数,提供者方法应该 return a 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)
);
}
在你的情况下你可以 return 一个 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}
}