Using the Maven Surefire plugin to run unit tests

A best practice of software development is writing automated unit tests for the code that you develop. Let us now see how to run these tests.

The plugin that does this job is the Maven Surefire plugin.

How to do it...

To run unit tests using the Maven Surefire plugin, perform the following steps:

  1. Open the command prompt.
  2. Run the following command on one of our sample projects:
    mvn test
    
  3. Observe the various steps that get executed:
    [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ simple-project ---
    [INFO] Surefire report directory: C:projectsapache-maven-cookbooksimple-project	argetsurefire-reports
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.packt.cookbook.AppTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
    Results:
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    

How it works...

The test parameter indicates the invocation of the default lifecycle to Maven. As illustrated in the Understanding the Maven lifecycle, phases, and goals recipe in Chapter 3, Maven Lifecycle, Maven runs all the phases up to and including the test phase, in order.

The test phase itself essentially runs the test goal of the Maven Surefire plugin.

This runs the test classes that are present in the target/test-classes folder.

The test that we have is a test written using the JUnit framework. Not only does the plugin run the test, it also generates a test report that can be used to analyze failures as well as test coverage.

Check the surefire-reports folder:

How it works...

While the text file contains the summary report, the XML file has the details of each of the tests.

There's more...

The Surefire plugin provides many configurations to make testing easier.

Using TestNG

JUnit is not the only way to write automated unit tests. You could use TestNG (http://testng.org) or even write your tests without using any framework (by using Java asserts).

Surefire determines the framework to be used based on the dependencies that have been defined.

Our earlier example ran JUnit tests because we had defined the junit dependency in the pom file.

Let us now write a test using TestNG and see what needs to change for it to work. Refer to the Maven project with TestNG.

The only change in the pom file is to replace the junit dependency with testng:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8.8</version>
    <scope>test</scope>
</dependency>

Run the following command on command prompt:

mvn test

The tests are now run in using TestNG:

[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ project-with-testNG---
[INFO] Surefire report directory: C:projectsapache-maven-cookbookproject-with-testNG	argetsurefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.packt.cookbook.AppTest
Set up run
Fast test
Slow test
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.609 sec

Now, examine the surefire-reports folder. It has a different set of files corresponding to testng:

Using TestNG

The same tests work with TestNG and JUnit as TestNG can run JUnit tests.

Skipping tests

There may be situations where you might not want to run the tests; some tests are possibly broken. This can be done in the following ways:

  • Configuring the Surefire plugin in the pom file: Configure your Surefire plugin in the pom.xml file using the following code:
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
              <skipTests>true</skipTests>
            </configuration>
          </plugin>
        </plugins>

    Now, run the following command:

    mvn test
    

    You will see the following output:

    [INFO]
    [INFO] --- maven-surefire-plugin:2.17:test (default-test) @ project-with-tests-skipped ---
    [INFO] Tests are skipped.
    
  • Issuing an mvn command with a command-line parameter: The tests can be skipped even by issuing the following command:
    mvn –DskipTests tests
    

Skipping the compilation of test sources

The skipTests parameter used in the preceding mvn command skips running of tests, but the test sources still get compiled by the earlier phases/goals. To skip the compilation of test sources, you can run the following command:

mvn –Dmaven.test.skip=true package

This will completely skip the test compilation and test execution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.228