In my TestClass
I have a lot of different test methods that all have to run with the same data and therefore have the same @MethodSource
:
public class TestClass {
private class TestData { /* some fields */ }
private List<TestData> testDataTemplates = asList(
new TestData(...),
/* some more elements */
);
static private Stream<Arguments> testDataProvider {
List<TestData> testData = new ArrayList<>();
// Here we do some nested .forEach iterating over the testDataTemplates
return testData.stream();
}
@ParametrizedTest
@MethodSource("testDataProvider")
public void testMethod1(...) { }
// some more test methods
@ParametrizedTest
@MethodSource("testDataProvider")
public void testMethodN(...) { }
}
For testMethodN
it is necessary to have the data a tiny bit different than for the other methods. My idea was to detect inside testDataProvider
for which method it is currently generating data and adapt the output accordingly.
Is there a way to do so?
If not, what it the cleanest way to achieve what I need to, underlining again, that the output of testDataProvider
for testMethodN
is really really similar to the output for the other test methods?
One could solve this using a new @MethodSource
:
static private Stream<Arguments> filteredTestDataProvider() {
List<Arguments> testCases = new ArrayList<>();
testDataProvider().forEach(args -> {
// decide whether and how the current args are used
});
return testCases.stream();
}
And, of course, the @MethodSource("filteredTestDataProvider")
of testMethodN
should be adapted accordingly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.