为什么Maven无法找到任何测试?

时间:2018-04-20 05:44:30

标签: maven tdd java-9 junit5

我有以下测试类

package com.mk.calculator.add;

import org.junit.jupiter.api.Assertions;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.util.ArrayList;
import java.util.List;

public class AddFunctionTest {
    @ParameterizedTest
    @CsvSource({ "-1, 1, 0", "0,0,0", "-100,200,100","100,400,500" })
    public void shouldAddTwoNumbers(final int first,final int second, final long result){
        AddFunction af = new AddFunction();
        List<Number> input = new ArrayList<>();
        input.add(first);
        input.add(second);
        Number output = af.calculate(input);
        Assertions.assertEquals(result,output);
    }
    @Test
    public void shouldAddMultipleNumbers(){
        AddFunction af = new AddFunction();
        List<Number> input = new ArrayList<>();
        input.add(1);
        input.add(2);
        input.add(3);
        input.add(4);
        input.add(5);
        input.add(6);
        input.add(7);
        input.add(8);
        input.add(9);
        input.add(10);
        Number output = af.calculate(input);
        Assertions.assertEquals(55l,output);
    }
    @Test
    public void shouldReturnSameNumber(){
        AddFunction af = new AddFunction();
        List<Number> input = new ArrayList<>();
        input.add(1);
        Number output = af.calculate(input);
        Assertions.assertEquals(1l,output);
    }
    @Test
    public void shouldReturnZero(){
        AddFunction af = new AddFunction();
        List<Number> input = new ArrayList<>();
        Number output = af.calculate(input);
        Assertions.assertEquals(0l,output);
    }
    @Test
    public void shouldworkOnNegatives(){
        AddFunction af = new AddFunction();
        List<Number> input = new ArrayList<>();
        input.add(-1);
        input.add(-9);
        Number output = af.calculate(input);
        Assertions.assertEquals(-10l,output);
    }
}

遵循Maven配置:

<?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">
    <parent>
        <artifactId>calculator</artifactId>
        <groupId>com.mk.calculator</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>AddFunction</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.mk.calculator</groupId>
            <artifactId>ifunction</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
           <version>5.1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.1.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

maven找到了测试类,但没有在其中找到任何测试。但是,我可以从IDE执行测试。

[INFO] Surefire report directory: /Users/mkanwar/projects/personal/java9/calculator/AddFunction/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mk.calculator.add.AddFunctionTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.236 s
[INFO] Finished at: 2018-04-20T11:00:36+05:30
[INFO] Final Memory: 14M/247M
[INFO] ------------------------------------------------------------------------

enter image description here 请帮我确定一下我在这里缺少什么。

1 个答案:

答案 0 :(得分:4)

您需要配置Maven Surefire插件以使用junit-platform-surefire-provider,如:

<build>
    <plugins>
        ...
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <includes>
                    <include>**/Test*.java</include>
                    <include>**/*Test.java</include>
                    <include>**/*Tests.java</include>
                    <include>**/*TestCase.java</include>
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>${junit.platform.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

有关详细信息,请参阅:https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven