Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Jukito is the combined power of JUnit, Guice, and Mockito – used for simplifying testing of multiple implementations of the same interface.

In this article we’re going to see how authors managed to combine those three libraries to help us reduce a lot of boilerplate code, making our tests flexible and easy.

2. Setting Up

First, we’ll add the following dependency to our project:

<dependency>
    <groupId>org.jukito</groupId>
    <artifactId>jukito</artifactId>
    <version>1.5</version>
    <scope>test</scope>
</dependency>

We can find the latest version at Maven Central.

3. Different Implementations of an Interface

To start understanding the power of Jukito, we’re going to define a simple Calculator interface with an Add method:

public interface Calculator {
    public double add(double a, double b);
}

And we’re going to implement the following interface:

public class SimpleCalculator implements Calculator {

    @Override
    public double add(double a, double b) {
        return a + b;
    }
}

We also need another implementation:

public class ScientificCalculator extends SimpleCalculator {
}

Now, let’s use Jukito to test both our implementations:

@RunWith(JukitoRunner.class)
public class CalculatorTest {

    public static class Module extends JukitoModule {

        @Override
        protected void configureTest() {
            bindMany(Calculator.class, SimpleCalculator.class, 
              ScientificCalculator.class);
        }
    }

    @Test
    public void givenTwoNumbers_WhenAdd_ThenSumBoth(@All Calculator calc) {
        double result = calc.add(1, 1);
 
        assertEquals(2, result, .1);
    }
}

In this example, we can see a JukitoModule, that wires in all specified implementations.

The @All annotation takes all bindings of the same interface made by the JukitoModule and runs the test with all the different implementations injected at runtime.

If we run tests, we can see that indeed two tests are run instead of one:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

4. The Cartesian Product

Let’s now add a simple nested class for different combinations of tests for our Add method:

public static class AdditionTest {
    int a;
    int b;
    int expected;

    // standard constructors/getters
}

This will expand the number of tests we can run, but first, we need to add additional bindings in our configureTest method:

bindManyInstances(AdditionTest.class, 
  new AdditionTest(1, 1, 2), 
  new AdditionTest(10, 10, 20), 
  new AdditionTest(18, 24, 42));

And finally we add another test to our suite:

@Test
public void givenTwoNumbers_WhenAdd_ThenSumBoth(
  @All Calculator calc, 
  @All AdditionTest addTest) {
 
    double result = calc.add(addTest.a, addTest.b);
 
    assertEquals(addTest.expected, result, .1);
}

Now the @All annotation is going to produce the Cartesian product of the different combinations between the different implementations of the Calculator interface and the AdditionTest instances.

We can have a look at the increased number of tests it now produces:

Tests run: 8, Failures: 0, Errors: 0, Skipped: 0

We need to remember that the number of test executions increases drastically for Cartesian products.

The execution time of all tests will grow linear with the number of executions. i:e.: a test method with three parameters with an @All annotation and four bindings per parameter will be executed 4 x 4 x 4 = 64 times.

Having five bindings for the same test method will lead to 5 x 5 x 5 = 125 executions.

5. Grouping by Names

The final feature we’ll discuss is the grouping by name:

bindManyNamedInstances(Integer.class, "even", 2, 4, 6);
bindManyNamedInstances(Integer.class, "odd", 1, 3, 5);

Here, we added some named instances of the integer class to our configureTest method, to showcase what can be done with these groups.

Now let’s add some more tests:

@Test
public void givenEvenNumbers_whenPrint_thenOutput(@All("even") Integer i) {
    System.out.println("even " + i);
}

@Test
public void givenOddNumbers_whenPrint_thenOutput(@All("odd") Integer i) {
    System.out.println("odd " + i);
}

The above example will print the six strings “even 2”, “even 4”, “even 6”, “odd 1”, “odd 3”, “odd 5”.

Keep in mind that the order of these is not guaranteed at runtime.

6. Conclusion

In this quick tutorial, we took a look at how Jukito allows the use of a whole test suite, by providing just enough combinations of test cases.

The complete example can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.