Can I have more than one method with @Parameters in junit test class which is running with Parameterized class ?
@RunWith(value = Parameterized.class)
public class JunitTest6 {
private String str;
public JunitTest6(String region, String coverageKind,
String majorClass, Integer vehicleAge, BigDecimal factor) {
this.str = region;
}
@Parameters
public static Collection<Object[]> data1() {
Object[][] data = {{some data}}
return Arrays.asList(data);
}
@Test
public void pushTest() {
System.out.println("Parameterized str is : " + str);
str = null;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = {{some other data}}
return Arrays.asList(data);
}
@Test
public void pullTest() {
System.out.println("Parameterized new str is : " + str);
str = null;
}
}
JUnit 5, the next generation of JUnit, facilitates writing developer tests with shiny new features. One such feature is parameterized tests. This feature enables us to execute a single test method multiple times with different parameters.
Show activity on this post. if (parameter == EXCEPTION_EXPECTED) { try { method(parameter); fail("didn't throw an exception!"); } catch (ExpectedException ee) { // Test succeded! } }
You can use the Theories runner (search for the word theories at that link) to pass different parameters to different methods.
Probably the data1
method, but no guarantee of that, it'll use whichever one the JVM gives junit4 first.
Here's the relevant code from junit:
private FrameworkMethod getParametersMethod(TestClass testClass) throws Exception {
List<FrameworkMethod> methods= testClass.getAnnotatedMethods(Parameters.class);
for (FrameworkMethod each : methods) {
int modifiers= each.getMethod().getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
return each;
}
throw new Exception("No public static parameters method on class " + testClass.getName());
}
So the first public, static annotated method that it finds will be used, but it may find them in any order.
Why do you have uour test written that way? You should only have one @Parameters
-annotated method.
It's not designated to have more than one data method. You can see it in skaffman's answer.
Why it's not provided to implement two data methods?
The answer could be: Coupling.
Is it too complex to split this test up into two testcases? You would be able to introduce a small inheritance and share common methods. With two testcases you could provide two separated data methods and test your stuff very well.
I hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With