如何运行测试maven?

时间:2017-04-14 14:25:37

标签: maven intellij-idea

我从maven任务调用单元测试时遇到问题。我的pom.xml

<?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>FizzBuzzTDDKata</groupId>
    <artifactId>FizzBuzzTDDKata</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit5-engine</artifactId>
            <version>5.0.0-ALPHA</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

我根据一些教程添加了<scope>属性,但是当我尝试使用mvn clean install或'mvn test'时,我得到了:

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.950s
[INFO] Finished at: Fri Apr 14 16:12:32 CEST 2017
[INFO] Final Memory: 11M/184M
[INFO] ------------------------------------------------------------------------

我正在使用Intellij IDEA 2016.2.5 Ultimate,每个maven任务都在Intellij中作为运行配置运行。我究竟做错了什么?我应该添加一些其他依赖项,构建到pom.xml吗?

1 个答案:

答案 0 :(得分:0)

首先,您需要添加

<build>
<plugins>
    ...
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19</version>
        <dependencies>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-surefire-provider</artifactId>
                <version>1.0.0-M4</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

按照Junit5 User Guide中的说明

到你的pom.xml。您必须定义一个TestEngine,它也在链接中描述。

<build>
<plugins>
    ...
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19</version>
        <dependencies>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-surefire-provider</artifactId>
                <version>1.0.0-M4</version>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.0.0-M4</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

...     ...              org.junit.jupiter         JUnit的木星-API         5.0.0-M4         测试     

这应该是全部。