无法使Maven运行JUnit测试

时间:2018-07-03 03:15:42

标签: java maven junit

我认为我已经尝试了旧版StackOverflow thread about this中的所有操作,但均未成功。

我有一个Java项目,在src / test / java中有一个junit jupiter测试类,称为SacmElementTest。如果我将其作为来自Eclipse的JUnit测试运行,则测试运行良好-其中有8个。但是,如果我从Maven(在Eclipse或命令行中)运行测试,一切似乎都很好,但是我得到了Tests run: 0, Failures: 0, Errors: 0, Skipped: 0,因此实际上并没有在运行测试。我的pom文件是:

<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>uk.co.tgrsafety</groupId>
<artifactId>gsn-editor-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gsn-editor</name>
<build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>

    <testSourceDirectory>src/test/java</testSourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <release>10</release>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.10.19</version>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-surefire-provider</artifactId>
        <version>1.2.0-M1</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.2.0-M1</version>
    </dependency>
</dependencies>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>

我在做什么错了?

2 个答案:

答案 0 :(得分:2)

请添加具有正确依赖性的maven surefire插件,如此处Junit 5 with Jupiter所述。

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.2.0</version>
        </dependency>
    </dependencies>
</plugin>

答案 1 :(得分:1)

Surefire插件的2.22.0版本具有对内置JUnit Jupiter的支持。根据其documentation,您必须将JUnit Jupiter引擎添加到项目依赖项中。

<project ...>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.2.0</version>
      <scope>test</scope>
    </dependency>
    ...
  </dependencies>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
      </plugin>
      ...
    </plugins>
  </build>
</project>