【问题标题】:mvn test is not running tests - my test cases work via IntelliJmvn test 没有运行测试 - 我的测试用例通过 IntelliJ 工作
【发布时间】:2020-01-12 13:47:48
【问题描述】:

我知道关于这个问题有多个线程,我已经尝试了长达 3 个小时,所以我为重复道歉。

我想要做的就是运行 mvn test 让它运行 Junit 5 测试。在 IntelliJ 上按 Run 有效,但终端中的 mvn test 无效。

文件结构如下 -

https://user-images.githubusercontent.com/37623164/64642711-53cf1d80-d452-11e9-9764-849d73fcc5c9.png

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.petrego</groupId>
    <artifactId>PetRego</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>PetRego</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.4.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>

</project>

在日志中返回结果 -

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< com.petrego:PetRego >-------------------------
[INFO] Building PetRego 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ PetRego ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ PetRego ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ PetRego ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/639603/projects/PetRego/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ PetRego ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.0:test (default-test) @ PetRego ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.704 s
[INFO] Finished at: 2019-09-11T05:02:30+10:00
[INFO] ------------------------------------------------------------------------

【问题讨论】:

  • 你读过这个吗:maven.apache.org/surefire/maven-surefire-plugin/examples/…?您是否使用来自 org.junit.jupiter.api.Test (JUnit5) 而不是 org.junit.Test (Junit4) 的正确注释?
  • 使用调试 (-X) 标志运行时会发生什么?
  • @AKSW 我改变了仍然返回相同的结果
  • @jhell 发现了某种冲突 - 现在调查。如果这是问题,我会感到惊讶,但让我们看看!有什么我应该特别寻找的吗?
  • 问题只是你必须将 junit-jupiter-engine 作为依赖项而不是别的......你应该检查你是否正确使用了 junit jupiter 的正确导入,而不是来自 junit 4 ...进一步以测试源为例...

标签: java spring maven testing junit


【解决方案1】:

一般来说,您应该做的第一件事是检查是否有更新版本的插件。 升级surefire应该可以解决您的问题,请参阅https://maven.apache.org/surefire/maven-surefire-plugin/

【讨论】:

  • 我也试过新版本。您在哪里看到版本不匹配?
  • 日志显示[INFO] --- maven-surefire-plugin:2.22.0:test (default-test),因此您使用的不是最新版本。
【解决方案2】:

正如@AKSW 在问题中建议的那样,导入不正确。

当我应该导入 JUnit5 org.junit.jupiter.api.Test (JUnit5) 时,我正在导入 org.junit.Test (Junit4)

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 2012-10-13
    • 2020-03-15
    • 2019-07-31
    • 2014-11-11
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多