@parameters on June 4

Is it possible to use several methods with @Parameters in the junit test class, which works with a 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; } } 
+6
junit junit4 annotations
source share
4 answers

You can use Theories runner (search for dictionary theories from this link) to pass different parameters to different methods.

+2
source share

Probably the data1 method, but does not guarantee this, it will use the one that the JVM first provides junit4.

Here is 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()); } 

Thus, the first public, static annotated method will be used, which he will find, but he can find them in any order.

Why do you have your test written this way? You should have only one @Parameters -annotated method.

+2
source share

It does not specify more than one data method. You can see it in the response of the scaffman .

Why is this not provided for the implementation of two data methods?
The answer may be: Communication.

Is it difficult to split this test into two test files? You could introduce a little inheritance and use common methods. With two test tests, you could provide two separated data methods and test your stuff very well.

Hope this helps.

+2
source share

You can create inner classes for each set of methods that work with the same parameters. For example:

 public class JunitTest6 { @RunWith(value = Parameterized.class) public static class PushTest{ private String str; public PushTest(String region) { this.str = region; } @Parameters public static Collection<Object[]> data() { Object[][] data = {{some data}} return Arrays.asList(data); } @Test public void pushTest() { System.out.println("Parameterized str is : " + str); str = null; } } @RunWith(value = Parameterized.class) public static class PullTest{ private String str; public PullTest(String region) { this.str = region; } @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; } } } 
+1
source share

All Articles