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.