【问题标题】:Spring Boot - My unit tests are being skippedSpring Boot - 我的单元测试被跳过
【发布时间】:2014-11-06 23:11:47
【问题描述】:

我正在迁移现有项目以启动。我使用 start.spring.io 创建了一个全新的项目并复制了源代码等。一切都可以编译,但是当我执行“mvn test”时,它会编译类但只执行默认的“ApplicationTests”(由 start 创建.spring.io)。

这是 maven 输出的摘录:

    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pendview ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory C:\dev\pendview2\src\test\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pendview ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 26 source files to C:\dev\pendview2\target\test-classes
    [INFO]
    [INFO] --- maven-surefire-plugin:2.15:test (default-test) @ pendview ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------

更奇怪的是,如果我通过了“-Dtest=TestAuthController”,那么它确实会运行特定的单元测试:

    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pendview ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 26 source files to C:\dev\pendview2\target\test-classes
    [INFO]
    [INFO] --- maven-surefire-plugin:2.15:test (default-test) @ pendview ---
    [INFO] Surefire report directory: C:\dev\pendview2\target\surefire-reports

    (skipped output of AuthControllerTest for brevity)

    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------Results :

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

我做错了什么? Spring Boot 是否设置了我不符合的安全配置?

任何帮助将不胜感激! -特雷

【问题讨论】:

    标签: java spring maven spring-mvc spring-boot


    【解决方案1】:

    在您的控制台/终端中尝试此命令:

    mvn clean install
    

    但首先导航到您的测试目录。 如果您想跳过测试,请使用以下命令:

    mvn clean install -Dmaven.test.skip
    

    【讨论】:

      【解决方案2】:

      看来 Andy 的答案应该针对 Spring Boot 2.X 进行更新。我现在在任何地方都看不到surefire配置。可能对这个公约有太多的抱怨。

      【讨论】:

        【解决方案3】:

        Spring Boot 将 Surefire 插件配置为运行名称以 TestTests 结尾但不以 Abstract 开头的所有测试类。你可以看到这个configuration in the spring-boot-starter-parent pom。如果您的测试类名为TestAuthController,那么它与此配置不匹配。将其重命名为 AuthControllerTestAuthControllerTests 应该可以解决您的问题。

        【讨论】:

        • 我也认为自 Spring Boot 2.4 以来 JUnit 4 测试被忽略
        • 从 2.4 开始,默认情况下不再包含 JUnit 的老式引擎。这意味着基于 JUnit-4 的测试将不会运行(并且可能也不会编译)。如果您无法迁移到 JUnit 5,您可以将老式引擎恢复为 described in Spring Boot's release notes
        【解决方案4】:

        在我看来,Maven surefire 插件存在一些问题,当它不检测测试时,如果您的测试类名称不以 Tests 后缀结尾。 :-)

        【讨论】:

          猜你喜欢
          • 2016-05-28
          • 2015-12-20
          • 1970-01-01
          • 2017-02-08
          • 2020-08-08
          • 2018-12-02
          • 2016-03-15
          • 2020-06-25
          相关资源
          最近更新 更多