Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@parameters in Junit 4

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;
 }
}
like image 290
ravinikam Avatar asked Jul 30 '09 12:07

ravinikam


People also ask

How is a parameterized test annotated in JUnit?

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.

How do I test exceptions in a parameterized test?

Show activity on this post. if (parameter == EXCEPTION_EXPECTED) { try { method(parameter); fail("didn't throw an exception!"); } catch (ExpectedException ee) { // Test succeded! } }


3 Answers

You can use the Theories runner (search for the word theories at that link) to pass different parameters to different methods.

like image 87
Yishai Avatar answered Sep 21 '22 13:09

Yishai


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.

like image 20
skaffman Avatar answered Sep 21 '22 13:09

skaffman


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.

like image 44
guerda Avatar answered Sep 22 '22 13:09

guerda