Avoid using maven.test.skip

You might manage an extremely small project that does not evolve a lot without unit tests. However, any large-scale project cannot exist without unit tests. Unit tests provide the first level of guarantee that you do not break any existing functionality with a newly introduced code change. In an ideal scenario, you should not commit any code to a source repository without building the complete project with unit tests.

Maven uses the surefire plugin to run tests, and as a malpractice, developers skip the execution of unit tests by setting the maven.test.skip property to true:

$ mvn clean install –Dmaven.test.skip=true

This can lead to serious repercussions in the later stage of the project, and you must ensure that all your developers do not skip testing while building.

Using the requireProperty rule of the Maven enforcer plugin, you can ban developers from using the maven.test.skip property. The following shows the enforcer plugin configuration that you need to add to your application POM:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>1.3.1</version>
  <executions>
    <execution>
      <id>enforce-property</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <requireProperty>
            <property>maven.test.skip</property>
            <message>maven.test.skip must be specified</message>
            <regex>false</regex>
            <regexMessage>You cannot skip tests</regexMessage>
          </requireProperty>
        </rules>
        <fail>true</fail>
      </configuration>
    </execution>
  </executions>
</plugin>

Now, if you run mvn clean install against your project, you will see the following error message:

maven.test.skip must be specified

This means you need to specify -Dmaven.test.skip=false every time you run mvn clean install:

$ mvn clean install –Dmaven.test.skip=true

However, if you set - Dmaven.test.skip=false, then you will see the following error:

You cannot skip tests

You will still find it a bit annoying to type Dmaven.test.skip=false whenever you run a build. To avoid this, add the maven.test.skip property in your application POM file and set its value to false:

<project>
  <properties>
    <maven.test.skip>false</maven.test.skip>
  </properties>
</project>

Note

More details about the requireProperty rule are available at http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html.

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

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