Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized JUnit tests with non-primitive parameters?

There is a nice possibility to run JUnit test with parameters where the same test method is executed multiple times with different data as described here: http://junit.org/apidocs/org/junit/runners/Parameterized.html

Unfortunately, it only seems possible to use primitive parameters or Strings, but not objects. Is there any workaround known for this?

like image 913
datka Avatar asked Feb 09 '12 17:02

datka


1 Answers

The type of the data() method in the use of the @Parameters annotation is List<Object[]>, so you can put in any object.

To pass in, e.g., a Money object, your array to be converted to a list would be:

{ { new Money(26, "CHF") }, { new Money(12, "USD") } }

The constructor of the test class should take a Money object as argument then.

like image 156
avandeursen Avatar answered Jan 03 '23 14:01

avandeursen