Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 706
Joel Rives Avatar asked Jul 10 '18 16:07

Joel Rives


People also ask

Why your JUnit 4 tests are not running under Maven?

Incorrect Test Folder The test directory contains the test source code. Any tests placed under the src/main/java directory will be skipped. Instead, all the tests and test resources should be placed under the src/test /java and src/test/resources folder, respectively.

How do you run a JUnit test case in Maven build?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

How do I run a test in Maven?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”. As the output shows, only the test class we've passed to the test parameter is executed.


2 Answers

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>
like image 199
codeCruncher Avatar answered Sep 18 '22 08:09

codeCruncher


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>
like image 25
Wes Grant Avatar answered Sep 20 '22 08:09

Wes Grant