Sunday, October 12, 2014

Parametirized test. JUnit. JUnitParams.

I've faced with one very interesting opportunity during experiments with unit testing in Java.
Many thanks to codeabbey for a huge list of interesting tasks that allows me to extend my knowledge about unit testing.
First of all JUnit documentation has a lot of examples how to create parametirized tests. This page contains simple examples that very easy to understand.
But, current implementation of parametirized tests has one disadvantage.
You cannot use different set of parameters for different tests.
For example you implement some specific algorithms for sorting of array and searching for a value inside of array. You would not like separate unit tests to different classes and you want to have several input data sets for both sorting and searching. Or you would like to test behaviour of sort algorithms on Numeric data types and String data types separately. With current realization in JUnit you have no possibility to setup two or more different set of parameters. In other words, you cannot specify set of parameters for each unit test.
After searching in Google, I've found one interesting implementation of such possibility: using JUnitParams extension.
Following several examples can show how to use and how to manipulate with such tests:

  1. Official WiKi page on github.com
  2. Example from old project page code.google.com or new project page on github.com
Project has pom.xml file and can be build from master by Maven. Some corrections still required.
To make successful build for my unit testing I did the following:

  1. Comment following code in pom.xml, to avoid installation gpg on my computer:
  2. Change build version since latest version from master required java-1.8
If you will follow all steps from examples, than all will works fine.
It was very useful for me to find such package and use it in my unit tests.