简体   繁体   中英

Spring Boot maven unit tests not being executed

I have a Spring Boot project. I have written unit tests in the.../src/test/java/... directories. All of my unit tests are of the form *Test.java. When I run 'mvn test', I get the following output:

[INFO]  
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ frm-api ---  
[INFO] Changes detected - recompiling the module!  
[INFO] Compiling 6 source files to /Users/JoelRives/Projects/fasor/fasor-service/fatality-review/target/test-classes  
[INFO]  
[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ frm-api ---  
[INFO]  
[INFO] -------------------------------------------------------  
[INFO]  T E S T S  
[INFO] -------------------------------------------------------  
[INFO]   
[INFO] Results:  
[INFO]  
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0  
[INFO]  

Clearly, maven sees the unit tests as it is compiling them. However, as you can see, it does not run them.

I have the following relevant maven dependencies in my pom:

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <version>1.5.3.RELEASE</version>
  </dependency>

  <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>test</scope>
      <version>1.4.194</version>
  </dependency>

My pom also includes the following plugin:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.3.0-M1</version>
                </dependency>
                <dependency>
                <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.2.0-M1</version>
                </dependency>
            </dependencies>
        </plugin>

Any help regarding this is greatly appreciated.

I did not show my entire POM. Here is the Junit stuff:

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
  </dependency>

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <version>1.5.3.RELEASE</version>
  </dependency>

  <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>test</scope>
      <version>1.4.194</version>
  </dependency>

For me, I had written this in my pom.xml and I was using JUnit4

        <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>

Once I removed the exclusions , it started executing.

It looks like if you are using Junit4 and the surefire plugin version 2.22 and above, the tests dont get picked up. I had a similar issue and using surefire V2.21.0 seemed to work.

Below is my surefire config

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.21.0</version>
            </plugin>

Try updating the surefire-plugin to pick the test classes explicitly as:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <includes>
                <include>**/*Test*.java</include>
            </includes>
        </configuration>
    </plugin>
</plugins>

You can look into this as well, may be duplicated!!

Note, this is a partial answer. I as able to get the unit tests to run by removing the surefire plugin from the POM. I have never had problems with surefire in the past. O well..., at least the tests are running -- broken as they may be :-) I will investigate the surefire issue later. Thanks for the suggestions.

Using recent surefire you can configure it to use JUnit 4:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>3.0.0-M5</version>
      </dependency>
    </dependencies>
  </plugin>

The key here is to understand the test engines that Spring uses.

In my case, I coded API tests with @RunWith(SpringRunner.class) , which is under JUnit4, which runs with junit-vintage-engine .

But the unit tests are coded with JUnit Jupiter, which is under JUnit5, which runs with junit-jupiter-engine .

The default engine of SpringBoot is the junit-jupiter-engine . So we have to tell to Spring that we also want to use junit-vintage-engine .

We can simply add the dependency in our pom.xml:

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

Or, if we want to use maven-surefire, we can add the dependency inside the plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId> 
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
  <dependencies>
    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <version>5.7.0</version>
    </dependency>
  </dependencies>
</plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM