Maven doesn't execute any unit test
Asked Answered
H

5

31

I am using Maven with multi-modules. There are 3 projects.

foo(the parent project)
foo-core
foo-bar

I configure all the dependencies and plugins in foo's pom:

<modules>
    <module>../foo-core</module>
    <module>../foo-bar</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            ...
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.14.1</version>
                <dependencies>
                    <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.14.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

There are several base classes and util classes for unit test in foo-core, so I add the maven-jar-plugin in foo-core project to make it available to foo-bar:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
               <execution>
                   <goals>
                       <goal>test-jar</goal>
                   </goals>
               </execution>
            </executions>
        </plugin>
    </plugins>
</build>

When I execute test goal, I got the result as follow:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
parallel='none', perCoreThreadCount=true, threadCount=2, useUnlimitedThreads=false

Results :

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

I do got tests in my projects. But why it doesn't run any of them?

Haematoblast answered 23/5, 2013 at 7:27 Comment(2)
Do names of test files comply with the ones listed on Surefire page? maven.apache.org/surefire/maven-surefire-plugin/examples/…Lippe
@Grzegorz Yes. All the test files are named ***Tests.java.Haematoblast
T
41

Rename test files from **Tests.java to **Test.java or add the following configuration to pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.14.1</version>
  <configuration>
    <includes>
      <include>**/*Tests.java</include>
    </includes>
  </configuration>
</plugin>
Torrens answered 23/5, 2013 at 7:43 Comment(5)
@Kirin Yao Don't worry! I knew the answer only because I did the same mistake at the beginning of every project. :-)Lippe
This doesn't work for me. I have one class "OptimizerTest.java" and it works perfectly without any inclusion, but the other class "OptimizerTest2.java" doesn't work. If i run mvn : test -Dtest=OptimizerTest i receive BUILD SUCCESS with 3 TESTs PASSED, if i run the same command but for OptimizerTest2 i receive BUILD FAILURE, test run: 0 where OptimizerTest2 is just a copy of the first one °_°Alguire
@Alguire OptimizerTest2.java is not included because the name ends with Test2 not with Tests.Lippe
You were right. The problem was the wrong import. I used org.junit.Test intends of org.testng.annotations.Test and so i repeatedly received errors. Anyway, thank you ;-)Alguire
Is there any reason it can't just scan all classes in src/test/java and look for @Test annotated methods by default?Vitellin
I
9

Additionally, one more cause for such a behaviour is if you have an exclusion for junit-vintage-engine in your pom.xml dependencies.

In my case I had excluded it from the spring-boot-starter-test dependency in my pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
  </exclusions>
</dependency>

This resulted in the same problem. Maven not able to recognize/find any test cases in the project even though they are present.

So just remove the exclusions section and run the maven command again.

Infracostal answered 30/6, 2020 at 15:39 Comment(0)
T
6

Had similar issue was able to run unit test case by adding dependency to sunfire plugin

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit4</artifactId>
                    <version>2.22.0</version>
                </dependency>
            </dependencies>
        </plugin>

above worked fine byb executing test cases on mvn install , mvn test and all

Toneytong answered 19/3, 2021 at 6:47 Comment(0)
J
1

In my case, it was necessary to add the word "Test" to the end of the name of all files with tests. For example, if you have "LoginTestNegative" then this will not work. You need to rename this to LoginNegativeTest. And now it will work.

Jacobo answered 12/4, 2021 at 19:1 Comment(0)
D
1

I had a similar problem today. I had converted most of my tests to JUnit5 and surefire was not executing them via mvn test. I had to modify the pom.xml entry for surefire to force it to use the JUnit5 engine as it was selecting JUnit4 by default

Dependencies section (vintage is for the JUnit4 tests)

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>${junit-jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>${junit-jupiter.version}</version>
    <scope>test</scope>
</dependency>

Plugin section

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M6</version>
    <configuration>
        <argLine>@{argLine}</argLine>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit-platform</artifactId>
            <version>3.0.0-M6</version>
        </dependency>
    </dependencies>
</plugin>
Dispose answered 27/4, 2022 at 13:20 Comment(2)
what's the value for @{argLine} ?Liberty
@ShrikantPrabhu Sorry, I only just saw this. It's probably too late for you. This line is a configuration value from our Jenkinsfile. You can probably leave that part out. In the Jenkinsfile we have this as a step in the Build stage sh '''mvn -B -ntp clean package -DargLine="-Xmx256m" '''Dispose

© 2022 - 2024 — McMap. All rights reserved.