Grouping DataPoints in Junit 4.12

Theories is one of my most used features of JUnit. It is very useful for writing tests for behaviour with a wide range of parameters. JUnit 4.12 introduces named data points. This is useful grouping the data points in the tests. For example, you can use it to group formats for date and time separately like this:

@DataPoint("date")
public static final String SEPARATED_DATE = "yyyy-mm-dd";

@DataPoint("date")
public static final String UNSEPARATED_DATE = "yyyymmdd";

@DataPoint("time")
public static final String SEPARATED_TIME = "hh:mm:ss";

@DataPoint("time")
public static final String UNSEPARATED_TIME = "hhmmss";

@Theory
public void test(@FromDataPoints("date") String dateFormat, 
  @FromDataPoints("time") String timeFormat) {

  ...
}

The runner will pass the values SEPARATED_DATE and UNSEPARATED_DATE as the dateFormat parameter and SEPARATED_TIME and UNSEPARATED_TIME as the timeFormat parameter. No more needing to wrap the parameters in some kind of object or having to test the parameters inside the tests! Furthermore, the runner will also automatically generate all possible values for enumerations and boolean values, so you no longer need to define them as data points.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: