Fork me on GitHub


What is Powerunit?

Powerunit is a java test unit framework, baed to work with the JDK 1.8.


What is the syntax of a Test?

A simple test look like :

import ch.powerunit.Test;
import ch.powerunit.TestSuite;

public class HelloWorldTest implements TestSuite {
  public void testHelloWorld1() {
    assertThat("Hello world").is(containsString(" "));

A test using parameter will look like

import java.util.Arrays;

import ch.powerunit.Parameter;
import ch.powerunit.Parameters;
import ch.powerunit.Test;
import ch.powerunit.TestSuite;

public class HelloWorldParameterTest implements TestSuite {
  @Parameters("Input string is %1$s, subString idx is %2$s, expected result is %3$s")
  public static Stream<Object[]> getDatas() {
    return Object[][] { { "ab", 0, "ab" }, { "ab", 1, "b" } });

  public String inputString;

  public int inputIndex;

  public String expectedString;

  public void testSubString() {


How are managed the before and after action of a test.

Compared to JUnit that use method annotated with @Before and @After, Powerunit try to avoid this approach in favor of a more JDK 1.8 approach (using lamdba), but also to have more clear test execution. To do so, the concept of rule (also from JUnit), is use as the only way to define element around the test.

Each test classes can contain one single field annotated with @Rule. This field defines the various modification of the test execution, and provides way to define the code to be run before or after the test. For example, to define a before action, the following syntax can be used :

@Rule public final TestRule level2 = before(this::prepare);

This will define that the method prepare (void, without parameter) will be used before the test. Next action can be chained by using the .around(next rule). For instance to have a before and a after, it is possible to use :

@Rule public final TestRule level2 = before(this::prepare).around(after(this::clean));    



How is done the integration with Hamcrest ?

The short answer is that hamcrest matchers (and powerunit matchers) are available from the various method of the TestSuite interface that must be implemented by the test classes.

The long answer is a quiet more complex :

  1. First of all, the hamcrest library is defined as a maven dependency of powerunit. Specifically the hamcrest-all is used as a dependency.
  2. It is of course possible to use the DSL classes of hamcrest directly.
  3. The TestSuite interface exposes almost all the methods provided by org.hamcrest.Matchers. It also add more matchers :
    • Matchers related to regular expression (matchesRegex).
    • Matchers related to exception (exceptionMessage).
    • Matchers related to the optional class of java (optional...).
    • Matchers related to the file (file...).