5

I am relatively new to Maven. I have done a lot of research and digging on this topic, but I can't seem to find an answer, so I thought I would ask here.

Goal: I would like to run mvn clean install test while skipping integration tests, as well as one particular unit test class.

I have tried the following:

mvn clean install -DskipITs -Dtest=!MyTestClass test

mvn clean install -DskipITs&&test=!MyTestClass test

mvn clean install -DskipITs&test=!MyTestClass test

However, none of the above commands seem to work. The first command of the three above made the most sense to me, but it seems as though the integration tests are being run when using that command. This is where my knowledge and understanding of Maven has a gap; I'm not sure if that's the expected behavior, or if that is the appropriate way to pass multiple properties on the command line?

When I run this command: mvn clean install -DskipITs test, the integration tests are successfully skipped.

I am familiar with the Maven build life-cycle, but it is possible that I am misunderstanding something or missing a detail.

Daniel
  • 2,345
  • 4
  • 19
  • 36
  • I believe this Answer might help you: https://stackoverflow.com/questions/9123075/maven-how-can-i-skip-test-in-some-projects-via-command-line-options – Austin Poole Feb 15 '19 at 17:17
  • You already know how to skip ITs. For the unit test, is using `@Ignore` (assuming JUnit) an option for you? – SiKing Feb 15 '19 at 17:28
  • Based on what you have written running: `mvn clean install test` will run several things duplicate...you can drill down that to `mvn clean install` but usually an `install` is not necessary. So you can do `mvn clean verify`... see also the docs about the maven life cycle: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html – khmarbaise Feb 16 '19 at 12:54

3 Answers3

6

Integration tests with maven are normally run with maven-failsafe-plugin

To tell this plugin to skip integration tests (make sure your integration test class names follow the convention *IT.java, otherwise you need to include them with <inclusions>), you can do that in the plugin's configuration, or from the command line (official doc):

mvn test -DskipITs

Single tests can be skipped with:

mvn test -Dtest=!MyTestClass

So this should work:

mvn clean install -DskipITs -Dtest=!MyTestClass
hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • 1
    You have written that you Integration tests in Maven are run with maven-surefire-plugin which is simply wrong. Maven Failsafe Plugin is intended to run integration-tests...In contradiction your link points to maven-failsafe-plugin so I suppose you have a type on your answer...(first sentences). – khmarbaise Feb 16 '19 at 11:45
  • 1
    yes, thanks, you're right - it was a thought-to-text mistake – hovanessyan Feb 16 '19 at 12:42
  • This answer suggests exactly the command that the OP listed as "not working". – Roland Illig Feb 16 '19 at 13:02
0

What worked for me was the following command:

mvn clean install -DskipITs "-Dtest=!MyTestClass, !**/*IT.java" test

I am still learning Java, but here is what I think happened in my case.

There are two plugins pertaining to testing in Java (there are probably many more, but these two were relevant to my issue): one is called "maven-failsafe-plugin", while the other is "maven-surefire-plugin". As @hovanessyan and others have pointed out, maven-failsafe-plugin typically runs integration tests, while maven-surefire-plugin typically runs unit tests (Maven docs reference).

In my case, when I would run the command mvn clean install -DskipITs -Dtest=!MyTestClass test, upon further digging in the logs, the integration tests would fail and I would receive the following additional buried error message:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test (default-test)

The integration tests for the project are found in a directory called integrationtests, and are named according to the convention "MyIntegrationTestIT.java". What eventually led me down the right track was this: Surefire docs. These docs describe the "test" argument that you can pass with a Maven command.

It seems to me that when I passed the argument -Dtest=!MyTestClass, it's as if that instructed the Surefire plugin to "don't run MyTestClass, but do run every other test file." Meanwhile, the -DskipITs argument instructed the Failsafe plugin to skip integration tests (which it had been doing all along). When I explicitly called out the test files that I didn't want to run, in the form "-Dtest=!MyTestClass, !**/*IT.java", Surefire understood exactly what I wanted to do. The Surefire plugin ran every test with the exception of MyTestClass and the integration tests, and the Failsafe plugin skipped the integration tests.

I don't fully understand why, in my case, the Surefire plugin was running the integration tests in the first place. Maybe it has to do with some config setting in the codebase I'm working with, or the naming convention of the integration test files, or some annotation (I'm still learning a lot about these things). I am sure that this answer could be edited to include even more helpful information or context that I don't yet have. In any case, hopefully these learnings are helpful for some other folks experiencing this issue.

Finally, it helped a lot when debugging to run the command mvn help:effective-pom and pass -X along with my mvn clean install test command.

Daniel
  • 2,345
  • 4
  • 19
  • 36
0

This configuration works for me:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <configuration>
      <excludes>
         <exclude>%regex[.*TestIT.*.class]</exclude>
      </excludes>
   </configuration>
</plugin>
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-failsafe-plugin</artifactId>
   <configuration>
      <includes>
         <include>%regex[.*TestIT.*.class]</include>
      </includes>            
   </configuration>
   <executions>
      <execution>
         <phase>test</phase>
         <goals>
            <goal>integration-test</goal>
            <goal>verify</goal>
         </goals>
      </execution>            
   </executions>
</plugin>