JUnit 5 Parameterized Tests

22 April 2017

This is a follow up to my previous post about data injection with JUnit. The JUnit team release milestone 4 of JUnit 5 on 1st of April 2017, which added a new module junit-jupiter-params to support parameterized tests within JUnit Jupiter.

The simplest way to provide different parameters to a test is via @ValueSource:

  static Collection<Object[]> data() {
    return Arrays.asList(new Object[][]{
      {17, false},
      {22, true}

  @MethodSource(names = "data")
  void personIsAdult(int age, boolean isAdult) {
    assertThat(new Person(age).isAdult(), is(isAdult));

Read more

Data Injection with JUnit

12 November 2016

JUnit 5 introduces dynamic test creation: A method annotated with @TestFactory is expected to return some kind of iterable ((e.g. List, Stream or Iterable) of type DynamicTest. This can be used to create random input data for a test.

  Stream<DynamicTest> dynamicTestCreation() {
    return new Random().ints(3, 0, 5).mapToObj(
      n -> dynamicTest("test " + n, () -> assertTrue(n < 5))

But dynamic tests are also a way to write parameterized tests. With this post I like to to compare the different ways of writing parameterized tests with JUnit.

Read more

TDD books recommendations

08 August 2016

In the last Coderetreat I attended, I introduced Test Driven Development (TDD) to my pairing partners. One of them asked me about book recommendations. This is the list I came up with:

Test Driven Development by Example - Kent Beck

Great book, easy to understand. Must read for TDD. Kent explain how to effectively use check lists (one of our session at the coderetreat) Example code is Java but easy to understand for every programmer.

Read more

Speaking at XPDays about JUnit 5

07 August 2016

I will be speaking about JUnit 5 at XPDays Germany on November 22nd. The talk will be in German but slides will be in English. I’ll post a link to the slides here after the talk. Hope to see you there!

Number.times() missing in Java 8

15 May 2014

To make life easier, I have some eclipse save actions enabled. One of them is Convert for loops to enhanced. So every time I save a java file, all iterator based foor loops are converted to for-each loops.

As I aim for warning free code, I was surprised that a new warning appeared in a different part of the class I edited. I headed over to the warning and found the following code:

for(Object object : aCollection) {

The warning said The value of the local variable object is not used. Appearently this is true. But why wasn't there a warning in the first place?

Read more

Java Constructors Never Return Null

29 January 2013

Once in a while I come across Java code bases where the a reference to a just created object is checked against null.
Often this occurs in unit tests like the following:

Foo foo = new Foo();

This is nonsense. An object creation never returns null. If something fails when instanciating the object, an exception is thrown.
Even if not enough memory is available to create the object an OutOfMemoryError is thrown instead.

BTW, this is different in languages like Objective-C and Smalltalk where the class instantiating methods (init, new) are ordinary class level methods which can of course return nil.

I recommend reading Object Initialization in Java by Bill Venners to get a deep understanding of this stuff.

Know your libraries: Map.values()

17 January 2013

In his great book Effective Java* Joshua Bloch describes the need to "Know and use the libraries" to write effective code.

The following is an example, where the knowledge of the standard Java libraries leads to much more elegant code.

The code I found was:

for (Map.Entry entry : getMap().entrySet()) {
	if(getMap().containsKey(entry.getKey)) {

So what does the code do? I'll refactor the code on the go so that you can see the steps.

Read more

Why compiler warnings matter.

11 January 2013

At a client I recently reviewed eclipse compiler settings and resulting warnings.
I stumbled upon a warning which I've never seen before: Empty control-flow statement.
As I'm always interested in new findings, I immediatly headed over to the source code:

Object object;
for (Long id : ids) {
	object = foo(id);
	if (object != null && bar(object))

eclipse annotates the semicolon in line 5 with the warning "Empty control-flow statement".
Looking at the code, it seems that there's either a bug or the code is way too complicated.

Read more

Older posts are available in the archive.