52

I've combed StackOverflow and many other sites, have found many other related posts and have followed all said suggestions, but in the end, failsafe is skipping my tests.

My JUnit test is located here: myModule/src/main/test/java/ClientAccessIT.java

I am skipping surefire because there are no unit tests in this module:

<!-- POM snippet -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
  <skip>true</skip>
  </configuration>
</plugin>

And I'm trying to run integration tests with failsafe:

<!-- POM snippet -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
        <execution>
            <id>run-tests</id>
            <phase>integration-test</phase>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

However, when I run mvn verify I see this:

[INFO] --- maven-failsafe-plugin:2.14.1:integration-test (run-tests) @ rest-services-test ---

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

I spent the last 4 1/2 hours scouring, any help would be appreciated. The only other thing that may be worth mentioning is that I have Cargo setting up and tearing down a Tomcat container. Does anybody see the glaring problem?

Matthias
  • 7,432
  • 6
  • 55
  • 88
dingalla
  • 1,219
  • 3
  • 12
  • 19

13 Answers13

29

Your tests are not in the default test sources directory src/test/java. See:

https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

myModule/src/main/test/java/ClientAccessIT.java

should be:

myModule/src/test/java/ClientAccessIT.java

You could also update your pom file (if you really wanted tests to live in main) to include:

<build>
    <testSources>
        <testSource>
            <directory>src/main/test</directory>
        </testSource>
    </testSources>
</build>
pro-tester
  • 371
  • 1
  • 4
  • 2
22

If you happen to use Spring Boot version 2.4 or higher, and your tests still use JUnit 4, remember to put this dependency:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
</dependency>

Version 2.4 of spring-boot-starter-test includes only JUnit 5 and removed the vintage engine that provided a TestEngine for running JUnit 3 and 4 based tests.

Without that vintage engine, all JUnit 4 tests are silently ignored.

neXus
  • 2,005
  • 3
  • 29
  • 53
  • Thanks neXus for this answer. It saved lot of time, since it was very difficult to find the cause, as no test where executed for me as all of the test where written in Junit4. – Suvojit Sep 24 '21 at 11:47
  • 1
    Or update to junit 5: https://stackoverflow.com/questions/65073944/it-are-no-longer-executed-with-failsafe-plugin-after-migrating-to-spring-boot-2/70991122#70991122 :) – rogerdpack Feb 04 '22 at 18:35
  • This was my issue. Some how whoever set up our integration tests got them on an old version of jUnit, while the unit tests were on jUnit 5 and worked just fine. So my mvn verify would execute unit test but int test would have 0 test found. – dsmithpl13 Mar 15 '23 at 17:18
18

I had a similar problem. If there aren't any test classes compiled to target/test-classes then check your pom file and ensure that the packaging isn't 'pom'.

Raymond
  • 406
  • 3
  • 9
  • What is "packaging isn't pom"? Any hint? – markthegrea Sep 12 '18 at 18:40
  • Look in your effective pom.xml for the tag "packaging". Look at this link for a bit more about the packaging tag. https://maven.apache.org/pom.html. the key is to ensure that the value of that tag is not "pom". – Raymond Sep 13 '18 at 21:55
17

For multi-module projects, the child project needs to have the plugin declared as followed,

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
 </plugin>

version inherits from parent pom. Without the above defined, the plugin in parent pom alone will not work.

barryku
  • 2,496
  • 25
  • 16
  • I had to define plugin inside parent's pom (pluginManagement section) and use it on the project's pom (plugins section) – Sergio Gabari Sep 04 '18 at 08:30
12

You need to rename your test class.

You can find the names the plugin looks for by default in the documentation, as pointed out by @acdcjunior:

By default, the Failsafe Plugin will automatically include all test classes with the following wildcard patterns:

  • "**/IT*.java" - includes all of its subdirectories and all java filenames that start with "IT".
  • "**/*IT.java" - includes all of its subdirectories and all java filenames that end with "IT".
  • "**/*ITCase.java" - includes all of its subdirectories and all java filenames that end with "ITCase".
Keppil
  • 45,603
  • 8
  • 97
  • 119
  • 4
    That does not seem to be true: http://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html - it says the default patterns are `**/IT*.java`, `**/*IT.java`, `**/*ITCase.java`. – acdcjunior May 05 '13 at 20:17
  • 1
    @acdcjunior beat me to it; here's a direct link to the relevant documentation page: https://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#includes – kryger May 05 '13 at 20:21
  • 2
    "**/*IT.java" - includes all of its subdirectories and all java filenames that end with "IT". Wouldn't ClientAccessIT.java meet the qualifications of this pattern? – dingalla May 05 '13 at 23:01
  • 32
    The example "ClientAccessIT.java" clearly matches *IT.java. So this answer is just useless and confusing. – Gustave Feb 15 '17 at 06:50
  • if you are usin JUnit 5 you can include them by filtering and using tags: https://maven.apache.org/surefire/maven-failsafe-plugin/examples/junit-platform.html# – Sergio Gabari Sep 04 '18 at 08:47
  • 4
    This answer is wrong and misleading. The correct answer was given by @pro-tester – t0r0X Feb 27 '20 at 23:20
9

I was having the same issue and tried a few of the suggested options here. I am developing a Spring Boot application using JUnit 5. Unfortunately, none of my integration tests (located in src/test/java and suffixed with *TestIT.java) were getting picked up, regardless of the advertising of the plugin to do so. I tried downgrading to 2.18.1 and 2.19.1 and tried both removing the <executions> and <configuration> sections to attempt to use even the plugin's default functionality, but no luck.

I finally changed the version to the latest (as of the time of writing) to 2.22.0, and viola. Here is the configuration I added to the <build><plugins> section of my pom. And even better, I don't need to add executions that define the integration-test and verify goals that you see most people include; failsafe executes these by default. I've included my surefire plugin configuration as well, since I am using it to execute unit tests during the test phase of the maven lifecycle.

Edit: I also had to ad the integration-test and verify executions to the plugin configuration as well.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.0</version><!--$NO-MVN-MAN-VER$-->
</plugin>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.22.0</version><!--$NO-MVN-MAN-VER$ -->
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
     </execution>
  </executions>
</plugin>

And just for good measure, to tell surefire and failsafe to use JUnit 5, I am including this in my <dependencies> section:

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <scope>test</scope>
</dependency>

Eclipse displays a warning (yellow squiggly underline) under the version tag, hence the inclusion of the <!--$NO-MVN-MAN-VER$ --> error. I'm sure there's a better way to deal with this, but it works for me in this case.

To speed up testing this configuration, and to avoid having to go through all of the earlier lifecycle phases before test, integration-test orverify phases, I used the following commands to quickly validate:

  • mvn failsafe:integration-test (runs only integration tests)
  • mvn surefire:test (runs only unit tests)
  • (I obviously run the entire lifecycle when it counts)

Hope this helps someone. Cheers!

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Danny Bullis
  • 3,043
  • 2
  • 29
  • 35
  • 1
    For me nothing was working without this junit-jupiter-engine dependency added in tests module. Thanks for the hint! Although I don't fully understand yet how it actually influenced it and made it work... Can anyone explain? When I started these tests by surfire-plugin (in default test phase), then the dependency wasn't needed and everything worked. – alwi Jul 17 '19 at 07:53
  • 1
    See the comment from Horatiu. He has the right solution, as by the Maven references: integration-test and verify – tibor17 Jul 02 '20 at 13:36
  • 1
    Interesting. I used to have to downgrade to 2.18.1. Which still works for me. Unless your tests are junit 5 then I have to use M-3.0.x https://stackoverflow.com/a/70991122/32453 – rogerdpack Feb 04 '22 at 18:56
6

I also had a similar problem but needed the packaging element to be "pom". Make sure your test source files are being compiled and added to the .../target/test-classes folder using the maven-compiler-plugin:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <executions>
                <execution>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
bonapart3
  • 469
  • 7
  • 20
4

For me it was a missing executions section

            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>

The failsafe with TestNG documentation at https://maven.apache.org/surefire/maven-failsafe-plugin/examples/testng.html had shown a pom without it, and it didn't work.

<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M3</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    [...]
</plugins>

Adding the missing executions section shown above solved the problem.

Gonen I
  • 5,576
  • 1
  • 29
  • 60
  • Of course we do not write all such details in the documenttaion because the specification related to the plugin goals are phase are related to the basic understanding of the Maven and plugin documentation and Maven basics. See the goals https://maven.apache.org/surefire/maven-failsafe-plugin/plugin-info.html, what defaul phase (acctualy failsafe does not have default phase but surefire has it) and then we expect that the user has a common understanding of these things. I would recommend separating the goal *integration-test* and *verify* under separate execution *id*. – tibor17 Jun 30 '20 at 14:10
1

I'm using java 8, failsafe plugin version 2.22.2 => tests not running problem, to solve this I added next:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-failsafe-plugin</artifactId>
     <version>${surefire.plugin.version}</version>
     <dependencies>
         <dependency>
             <groupId>org.apache.maven.surefire</groupId>
             <artifactId>surefire-junit47</artifactId>
             <version>2.22.2</version>
         </dependency>
    </dependencies>
     <executions>
         <execution>
             <id>integration</id>
             <phase>integration-test</phase>
             <goals>
                 <goal>integration-test</goal>
            </goals>
        </execution>
        <execution>
            <id>verify</id>
            <phase>verify</phase>
            <goals>
                <goal>verify</goal>
            </goals>
        </execution>
     </executions>
</plugin>
Horatiu
  • 375
  • 3
  • 6
0

I had somewhat similar issue. My Integration tests were not being picked up. My project is a multi module spring boot project. And my mistake was that I added maven-failsafe-plugin in the parent POM. After fiddling around with many options I placed the failsafe-plugin in the module pom where all my integration tests were present which solved my problem.

horizon7
  • 1,113
  • 14
  • 18
  • The goals **integration-test** and **verify** have to be listed in the execution(s) section of the plugin. Otherwise you won't see it running. The place: parent or child was not the answer why you made it running. – tibor17 Jun 30 '20 at 14:14
0

For me, surefire:verify did not run any tests. I had to use surefire:integration-test.

pcampana
  • 2,413
  • 2
  • 21
  • 39
cliffberg
  • 11
  • 2
  • Of course it won't work because **surefire** does not have the goals **verify** and **integration-test**, see the documentation https://maven.apache.org/surefire/maven-surefire-plugin/plugin-info.html – tibor17 Jul 02 '20 at 13:23
  • If you want to use these goals, you have to use **failsafe** plugin, and the commands will be in this order **mvn failsafe:integration-test** and **mvn failsafe:verify**, see the goals for failsafe in documentation https://maven.apache.org/surefire/maven-failsafe-plugin/plugin-info.html – tibor17 Jul 02 '20 at 13:25
0

Since there are so many pieces that can lead to this problem, I will share how mine was resolved.

There was an active profile in my maven settings file ( .m2/settings.xml ), it had maven.test.skip property set to true and that was skipping the tests when I run the tests via maven. It was needed for an old application and I totally forgot about that profile.

</profiles>
   <profile>
      <id>god-damnit-profile</id>
      <properties>
        ...
        <maven.test.skip>true</maven.test.skip>
      </properties>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>god-damnit-profile</activeProfile>
  </activeProfiles>

I commented it out and it works now.

<activeProfiles>
    <!-- <activeProfile>god-damnit-profile</activeProfile> -->     
</activeProfiles>
mcvkr
  • 3,209
  • 6
  • 38
  • 63
0

If you use JUnit5, don't forget to add the following dependency to your test classpath:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
    </dependency>
kolobok
  • 3,835
  • 3
  • 38
  • 54