2015-07-28 10 views
7

Mam reszta Spring Ramp API Spring Boot, który chcę przetestować. Mogę uruchomić testy ręcznie w Eclipse (bez maven i uruchamiając aplikację jako test JUnit) i działa dobrze i wyświetla wyniki, ale mvn test nie "działa", jak się dowiesz poniżej.Maven nie uruchamia testów Spring Boot

Oto mój plik POM:

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

<groupId>org.demo</groupId> 
<artifactId>rest</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>war</packaging> 

<name>UserRegistrationServices</name> 
<description>RESTful API</description> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.2.5.RELEASE</version> 
    <relativePath /> <!-- lookup parent from repository --> 
</parent> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <java.version>1.8</java.version> 
</properties> 

<dependencies> 

    <!-- Junit --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <scope>test</scope> 
    </dependency> 

    <!-- Spring dependencies --> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-mongodb</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 

    <!-- To deploy to external servlet container --> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-tomcat</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <!-- For Spring Boot testing --> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>com.jayway.restassured</groupId> 
     <artifactId>rest-assured</artifactId> 
     <version>2.4.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>com.jayway.restassured</groupId> 
     <artifactId>json-schema-validator</artifactId> 
     <version>2.4.1</version> 
     <scope>test</scope> 
    </dependency> 

    <!-- For returning objects as JSON --> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-core</artifactId> 
     <version>2.5.4</version><!--$NO-MVN-MAN-VER$ --> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml</groupId> 
     <artifactId>jackson-xml-databind</artifactId> 
     <version>0.6.2</version> 
    </dependency> 

    <!-- To decode Base64 data --> 
    <dependency> 
     <groupId>commons-codec</groupId> 
     <artifactId>commons-codec</artifactId> 
     <version>1.10</version> 
    </dependency> 
</dependencies> 

<pluginRepositories> 
    <pluginRepository> 
     <id>spring-snapshots</id> 
     <url>http://repo.spring.io/snapshot</url> 
    </pluginRepository> 
</pluginRepositories> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <executions> 
       <execution> 
        <goals> 
         <goal>repackage</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

jest to wynikiem 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] ------------------------------------------------------------------------ 

Oto Segment z mojej klasy TestController.java w src/test/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")); 
    } 

Na szczycie klasy TestController mam te adnotacje:

@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 {....} 

Nie jestem pewien, co jest nie tak. Nie mam wtyczki surefire, ale wygląda na to, że to wygląda.

Odpowiedz

22

Kod w klasie o nazwie TestController nie jest kontrolerem, to test, ale zgodnie z konwencją jest to kontroler (może być używany w testach). Domyślnie Surefire będzie szukał testów pasujących do *Test; zmień nazwę klasy na ControllerTest.

+0

Ważne oświadczenie o schemat nazewnictwa dla programowania test-driven, rzeczywiście! –

+0

Działa! Miałem ten sam problem i po zmianie nazwy na "... Test" Maven zaczyna wykonywać moje testy. –

4

Nawet jeśli nie jest to zalecane (a nie standard), można skonfigurować Maven murowany wtyczki też, jak następuje:

<plugins> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <configuration> 
      <includes> 
       <include>**/Test*.java</include> 
      </includes> 
     </configuration> 
    </plugin> 
</plugins>