Maven没有运行Spring Boot测试

我有一个我要测试的restSpring Boot REST API。 我可以在Eclipse中手动运行测试(没有maven并通过运行应用程序作为JUnit测试)并且它运行正常并显示结果,但mvn test不起作用,因为您将在下面找到。

这是我的POM文件:

http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0

 org.demo rest 0.0.1-SNAPSHOT war UserRegistrationServices RESTful API  org.springframework.boot spring-boot-starter-parent 1.2.5.RELEASE     UTF-8 1.8     junit junit test    org.springframework.boot spring-boot-starter-data-mongodb   org.springframework.boot spring-boot-starter-web   org.springframework.boot spring-boot-starter-security    org.springframework.boot spring-boot-starter-tomcat provided    org.springframework.boot spring-boot-starter-test test   com.jayway.restassured rest-assured 2.4.1 test   com.jayway.restassured json-schema-validator 2.4.1 test    com.fasterxml.jackson.core jackson-core 2.5.4   com.fasterxml jackson-xml-databind 0.6.2    commons-codec commons-codec 1.10     spring-snapshots http://repo.spring.io/snapshot      org.springframework.boot spring-boot-maven-plugin    repackage       

这是mvn test的结果:

 [INFO] Scanning for projects... [INFO] [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building UserRegistrationServices 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ rest --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ rest --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 9 source files to C:\Users\pmandayam\git\UserRegistrationServices\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ rest --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory C:\Users\pmandayam\git\UserRegistrationServices\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ rest --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:\Users\pmandayam\git\UserRegistrationServices\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.17:test (default-test) @ rest --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.768 s [INFO] Finished at: 2015-07-28T12:07:41-05:00 [INFO] Final Memory: 24M/212M [INFO] ------------------------------------------------------------------------ 

这是我在src / test / java中的TestController.java类的一部分:

 @Test public void f_findByUsername() { // Finding user with username 'user1username' given().auth().basic("User1username", "Testpassword").when().get( "http://localhost:8080/users/get/ByUsername?username=User1username") .then().assertThat().body("username", is("User1username")); } 

在TestController类的顶部,我有这些注释:

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration /* Tells the embedded Tomcat server to start on a random, open port */ @IntegrationTest("server.port:0") @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestController {....} 

我不确定什么是错的。 我没有surefire插件,但它似乎正在寻找它。

你命名为TestController的类中的代码不是控制器,它是一个测试,但约定说它是一个控制器(可能用于测试)。 默认情况下,Surefire将寻找匹配*Test ; 将类重命名为ControllerTest

即使不推荐这样做(非标准),您也可以配置maven surefire插件 ,如下所示:

   org.apache.maven.plugins maven-surefire-plugin   **/*Test*.java     

编辑:在/Test*.java之前添加的通配符

这可能发生的另一个原因是在你的pom中声明了另一个surefire插件。 在我的情况下,我将一个应用程序迁移到spring boot并将其保留在pom中。

   org.apache.maven.plugins maven-surefire-plugin  false  **/*Test*.java     clean  test     

从pom中移除此部件后执行Spring启动测试。