Saturday, April 27, 2013

Parametrized unit test for JUnit and Android (DataSet)

Developing for Android I found testing difficult. Especially when it comes to Unit Testing. Lets be honest Android doesn't provide options to do proper unit test (no, if it begins with "create an instrumentation test", it is not unit test). Third party frameworks (eg. Robolectric) can be used to mock Android classes. They require you to use their test runner, or extend that. This can't be combined with the Parametrized runner of JUnit (which could be used to run a bunch of input parameters against the same test method). Thankfully JUnit has an other powerful extension, called Rules. I've created a package to provide a Parametrized (or DataSet) like annotation.

DataSet package

  • can be used with any TestRunner (eg. with Robolectric Runner)
  • can have multiple datasets in the same test class (and different test methods can use either of them)
  • can set expected exceptions (and they can be different for each testVector)
  • can have run time generated dataset
Note: at least JUnit 4 is needed to use this, since Rules were introduced in that.

If you use it, I would be delighted, if you could let me know. It is always good feeling when someone else finds my work useful :-).