This is the classical HelloWorld sample. this example describes the basical elements of a test based on PowerUnit.
As a general warning, it is important to remarks that the version of the powerunit artifact is ommited in the sample
First of all, the maven compiler must be defined to use the java 1.8 :
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
Also, surefire must be defined to use the powerunit library :
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <dependencies> <dependency> <groupId>ch.powerunit</groupId> <artifactId>powerunit</artifactId> </dependency> </dependencies> </plugin>
Finally, the powerunit libary must be added as a dependency :
<dependency> <groupId>ch.powerunit</groupId> <artifactId>powerunit</artifactId> <scope>test</scope> </dependency>
As an example, a simple POJO class with one field is used as the under test class :
package ch.powerunit.demo; public class MyBean { private String field1; public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } }
Here is the test class :
package ch.powerunit.demo; import ch.powerunit.Rule; import ch.powerunit.Test; import ch.powerunit.TestRule; import ch.powerunit.TestSuite; public class MyBeanTest implements TestSuite { @Rule public final TestRule rule = before(this::prepare); private MyBean bean; private void prepare() { bean = new MyBean(); } @Test public void testSimple() { bean.setField1("x"); assertThat(bean.getField1()).is("x"); } }
This class will check that the value set using the setField1 method is available by using the getField1 method. The interesting elements are :
In the output of maven, the result of the test is display as a normal test by surefire :
Running ch.powerunit.demo.MyBeanTest (of <none>) Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec - in ch.powerunit.demo.MyBeanTest (of <none>) Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0