Spring Boot - 我的单元测试被跳过了

时间:2014-09-12 17:46:57

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

我正在迁移现有项目以启动。我使用start.spring.io创建了一个全新的项目,并复制了源代码等。一切都编译,但是当我进行'mvn测试'时,它编译类但只执行默认的'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是否设置了一个我不符合的确定配置?

任何帮助将不胜感激! -Trey

4 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:1)

对我来说,Maven surefire插件存在一些问题,如果您的测试类名称没有以Tests后缀结尾,则它没有检测到测试。 : - )

答案 2 :(得分:1)

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

mvn clean install

但首先导航到测试目录。 如果您不想运行测试,请使用此命令:

mvn clean install -Dmaven.test.skip

答案 3 :(得分:0)

似乎Andy的答案应该针对Spring Boot 2.X进行更新。我现在无法在任何地方看到surefire配置。关于这个公约可能抱怨太多了。

相关问题