General
Matchers
Powerunit is a java test unit framework, baed to work with the JDK 1.8.
A simple test look like :
import ch.powerunit.Test; import ch.powerunit.TestSuite; public class HelloWorldTest implements TestSuite { @Test() public void testHelloWorld1() { assertThat("Hello world").is(containsString(" ")); } }
A test using parameter will look like
import java.util.Arrays; import java.util.stream.Stream; 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 Arrays.stream(new Object[][] { { "ab", 0, "ab" }, { "ab", 1, "b" } }); } @Parameter(0) public String inputString; @Parameter(1) public int inputIndex; @Parameter(2) public String expectedString; @Test public void testSubString() { assertThat(inputString.substring(inputIndex)).is(expectedString); } }
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));
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 :