简体   繁体   中英

Maven doesn't find JUnit test to run

When I try to run my tests with maven ( mvn test ), it seems that maven doen't find any test to run:

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running calculator.CalculatorTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Results :

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

I have read this , this , this and this questions, but it doesn't solve my problem.

I have a simple Java project using Maven and JUnit. My project structure is the following:

\_ src
  \_ main
     \_ java
        \_ calculator
           \_ Calculator.java
  \_ test
     \_ java
        \_ calculator
           \_ CalculatorTest.java

In my CalculatorTest.java file, I have this:

    package calculator;
    import org.junit.jupiter.api.Test;
    import static org.junit.jupiter.api.Assertions.*;

    public class CalculatorTest {

        @Test
        public void addABPositive() {
            Calculator calculator = new Calculator();
            int a, b, res;
            a = 5;
            b = 5;
            res = a + b;
            if (calculator.add(a, b) != res) {
                fail("a and b positive");
            }
        }
    }

My pom.xml file is (auto-generated by IntelliJ):

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>HelloWorld</groupId>
        <artifactId>HelloWorld</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.0.0</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <properties>
            <maven.compiler.source>1.6</maven.compiler.source>
            <maven.compiler.target>1.6</maven.compiler.target>
        </properties>
    </project>

I don't know what I'm doing wrong. Maybe the dependencies, wrong version of JUnit ?

Thank for your help.

Based on the example project provided by the junit-team on github , the pom requires the surefire plugin to be configured:

<build>
    <plugins>
        <!-- JUnit 5 requires Surefire version 2.22.1 or higher -->
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </plugin>
    </plugins>
</build>

The are several issues. First compiler source/target 1.6 does not work cause Junit 5 needs JDK 8 minimum...

To run JUnit 5 tests you have to add the following dependency:

<dependencies>
    [...]
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.3.1</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>

Second as already mentioned you need to pin maven-surefire-plugin to minimum 2.22.1...

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