Unit Test Example

If you like to get your unit tests running you have to follow the naming conventions which are defined by the Maven Surefire Plugin.

Assume we have the following structure of a simple Java project which currently consisting of simple class BitMask.java and of course a unit test class BitMaskTest.java. Based on that we get the following folder structure in the unit test example.

.
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- soebes
    |               `-- training
    |                   `-- maven
    |                       `-- simple
    |                           `-- BitMask.java
    `-- test
        `-- java
            `-- com
                `-- soebes
                    `-- training
                        `-- maven
                            `-- simple
                                `-- BitMaskTest.java

For the sake of clarity the pom.xml excerpt contains only the relevant things. If you don't know what a scope and/or a dependency is, i recommend to take a look into the documentation about dependency mechanism in Maven.

In this case we use JUnit for writing the unit test. This will work as well with TestNG if you prefer TestNG over JUnit.

The following pom.xml snippet shows how to add the dependency of JUnit to your project.

38
39
40
41
42
43
44
45
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
  </dependency>
</dependencies>

If you like to execute the unit tests of the project, which can simply be achieved by calling mvn test, you will get a result like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building MaUI Test Guide :: Unit Test Example 0.1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ unit-test-example ---
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ unit-test-example ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/maui/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.4:compile (default-compile) @ unit-test-example ---
[INFO] Compiling 1 source file to /home/maui/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ unit-test-example ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.4:testCompile (default-testCompile) @ unit-test-example ---
[INFO] Compiling 1 source file to /home/maui/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12:test (default-test) @ unit-test-example ---
[INFO] Surefire report directory: /home/maui/target/surefire-reports
 
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.soebes.training.maven.simple.BitMaskTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec
 
Results :
 
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.713s
[INFO] Finished at: Sat May 25 18:45:43 CEST 2013
[INFO] Final Memory: 12M/149M
[INFO] ------------------------------------------------------------------------