Question

I'm relatively new to JUnit 4; I have figured out that I can repeat the same test over a method, with different inputs, using @Parameters annotation to tag a method returning an Iterable of arrays, say a List<Integer[]>.

I found out that JUnit does require the method provider of Iterable of arrays to be static and named data, which means that you can test different methods but using always the same data.

As a matter of fact you can tag any method (with any return type, BTW) with @Parameters, but with no effect; only data() method is taken into account.

What I was hoping JUnit allowed to do was having some different data sets annotated with @Parameters and some mechanism (say an argument to @Test) which could specify to use data set X while performing testFoo() and data set Y for testBar().

In other words I would like to set up a local (as opposed to class/instance) data set in each of my testing methods.

As I understand the whole thing, you are obliged to build a separate class for each of the methods you want to test with multiple inputs, which IMHO makes the thing pretty useless; AAMOF I built myself a modest framework (actually based on JUnit) which indeed allows me multiple methods testing with multiple inputs (with tracing feature), all contained in a single class, avoiding so code proliferation.

Am I missing something ?!?

No correct solution

OTHER TIPS

It shouldn't matter what the method is called, as long as it has the @Parameters annotation.

The easiest way to have multiple datasets is to put the tests in an abstract class and have the concrete subclasses have the method with the @Parameters method.

However, I would question why one would want to have multiple datasets per test in the same test class. This probably violates the single responsibility principle and you should probably break the data sets into 1 test class each.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top