Spring Boot - My unit tests are being skipped

一个人想着一个人 提交于 2019-12-23 08:34:51

问题


I am migrating an existing project to boot. I created a brand new project using start.spring.io and copied over the source code, etc. Everything compiles, but when I do a 'mvn test' it compiles the classes but then only executes the default 'ApplicationTests' (created by start.spring.io).

Here's an excerpt from the maven output:

    [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] ------------------------------------------------------------------------

What's even stranger is that if I pass '-Dtest=TestAuthController' then it does run that specific unit test:

    [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

What am I doing wrong? Does spring boot setup a surefire config that I'm not conforming to?

Any help would be greatly appreciated! -Trey


回答1:


Spring Boot configures the Surefire plugin to run all test classes that have a name ending with Test or Tests but not starting with Abstract. You can see this configuration in the spring-boot-starter-parent pom. If your test class is named TestAuthController then it doesn't match this configuration. Renaming it to AuthControllerTest or AuthControllerTests should fix your problem.




回答2:


Seems for me there is some issue with Maven surefire plugin, when it doesn't detect tests, if your test class name doesn't end with Tests suffix. :-)




回答3:


Try this command in your console/terminal:

mvn clean install

But first navigate to directory of your tests. If you wont to not run test use this command:

mvn clean install -Dmaven.test.skip




回答4:


It seems Andy's answer should be updated for Spring Boot 2.X. I can't see surefire config anywhere now. Probably there were too many complains about this convention.



来源:https://stackoverflow.com/questions/25813941/spring-boot-my-unit-tests-are-being-skipped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!