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:

public static final String SEPARATED_DATE = "yyyy-mm-dd";

public static final String UNSEPARATED_DATE = "yyyymmdd";

public static final String SEPARATED_TIME = "hh:mm:ss";

public static final String UNSEPARATED_TIME = "hhmmss";

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.

Mocking Java classes in Groovy vs Mockito

While experimenting with using an ODF parser in Groovy, I was attempting to write unit tests in Groovy and mocking objects from the ODF parser library was required in my tests. Prior to this, most of experience in writing unit test was in Java and mocking using Mockito. Since I had been working in Groovy, I decided to see what support Groovy provides for mocking and how it compares to Mockito (the documentation for mocking in Groovy is available on the Groovy Mocks page).

Read more of this post

[Vim] Diff and merge using Vim

After performing a system update on my Linux machine, the package manager placed a copy of an updated configuration file on the system – it couldn’t overwrite them as I had previously modified it. Since I was also using SSH to remote in the Linux machine, I didn’t want to merge the changes using a GUI like Meld. Fortunately, Vim supports diffing and runs within the terminal!

To view the diff between files, you can use vimdiff.

vimdiff {first file} {second file}

Read more of this post

[Junit] Including the parameters in the names of parameterized tests

Junit 4.11 has added the ability to include the name of the parameters in a Parameterized test. This is done by providing a String to the name argument of the @Parameters annotation. Here is an example of a test that uses this feature:

public class AdditionTest 
    @Parameters(name="{0} + {1}")
    public static Collection<Object[]> parameters() {
        Object[][] data = new Object[][] {{1, 2, 3}, {0, 3, 3}, {-1, 1, 0}};
        return Arrays.asList(data);
    private final int first;
    private final int second;
    private final int expected;
    public AdditionTest(int first, int second, int expected) {
        this.first = first;
        this.second = second;
        this.expected = expected;
    public void testAddition() {
        Assert.assertThat(first + second,;

The arguments are substitued into the numeric arguments of message. In the above example, the tests would be named “testAddition[1 + 2]“, “testAddition[0 + 3]” and “testAddition[-1 + 1]“. Using this feature, you would be able to tell what the parameters were without having to refer back to your unit test code.

Prior to 4.11 the tests would have been named “testAddition[0]“, “testAddition[1]” and “testAddition[2]“, where the 0, 1 and 2 correspond to the index in the Collection that was provided as the parameters – you have to refer back to your unit test code to figure out what the parameters were. If you do NOT specify the name in 4.11, the tests will also be named in this fasion.

[Bash] Reading from Standard Input

To read from standard input in a Bash shell, use read. The following script reads content from stanard input and outputs the content:


# Uncomment this to use comma as a separator.
# IFS=","

read first second third
echo first=\"${first}\" second=\"${second}\" third=\"${third}\"

Read more of this post

[Sed] Editing files in place

Sed provides the -i or --in-place option for saving changes made to the file. For example, you can replace all tabs with a space with the following:

sed 's/\t/ /g' -i file.txt

Read more of this post

[Grails] Creating a weekly view of a calendar

Lately, I’ve been trying to learn Grails and started a little project as part of the learning exercise. I started off by creating weekly view of a calendar. The code for rendering this calendar can be found from my Github repository. Note: if you just want to use a calendar in Grail, you should check out the Full Calendar plugin.

Read more of this post


Get every new post delivered to your Inbox.