2016-07-24 12 views
2

Meine Testklasse istfeder rest-docs Anwendung nicht Schnipsel zu erzeugen, wenn ausführen oder Integration getestet

package com.htc.spring.rest.docs; 
    ------- 
    @RunWith(SpringJUnit4ClassRunner.class) 
    @SpringApplicationConfiguration(classes=CrudRestDemoApplication.class) 
    @WebAppConfiguration 
    @IntegrationTest 
    public class ForRestDocumentationTest { 

    @Rule 
    public JUnitRestDocumentation restDoc = new  
     JUnitRestDocumentation("target/generated-snippets"); 

    private final ObjectMapper objectMapper = new ObjectMapper(); 

    @Autowired 
    public EmbeddedWebApplicationContext context;//or WebApplicationContext 

    private MockMvc mockMvc; 

    public ForRestDocumentationTest(){ 
    System.out.println("Test class created"); 
    } 

    @Before 
    public void setup(){ 
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) 
    .apply(documentationConfiguration(this.restDoc)).alwaysDo(document(" 
    {method-name}/{step}/")).build(); 


    } 

    @Test 
    public void addOrder() throws Exception { 
    GregorianCalendar calendar = new 
     GregorianCalendar(2015,Calendar.MARCH,21); 
     OrderTO newOrder = 
     new OrderTO(8000, calendar.getTime(), "M/s Joseph Sales", 2120.5); 

     this.mockMvc.perform(post("/orders") 
      .accept(MediaType.APPLICATION_JSON) 
      .content(objectMapper.writeValueAsString(newOrder)) 
      .contentType(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk()) 
      .andDo(document("index")); 
     } 


    @Test 
    public void getOrder() throws Exception { 
     System.out.println("get order fired"); 
     int orderId = 2000;  
     this.mockMvc.perform(get("/orders/{orderId}", orderId) 
      .contentType(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk()) 
      .andExpect(jsonPath("orderId").isNotEmpty()) 
      .andExpect(jsonPath("orderDate").isNotEmpty()) 
      .andExpect(jsonPath("customer").isNotEmpty()) 
      .andExpect(jsonPath("cost").isNotEmpty()) 
      .andDo(document("index")); 
     } 
    @Test 
    public void getAllOrders() throws Exception { 

     this.mockMvc.perform(get("/orders") 
      .contentType(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk()) 
      .andExpect(jsonPath("$").isArray()) 
      .andExpect(jsonPath("[*].orderId").isNotEmpty()) 
      .andExpect(jsonPath("[*].orderDate").isNotEmpty()) 
      .andExpect(jsonPath("[*].custoer").isNotEmpty()) 
      .andExpect(jsonPath("[*].cost").isNotEmpty()) 
      .andDo(document("index")); 
     } 
     private static final String ORDERS_ORDERID_DESCRIPTION = 
       "OrderTO's OrderId"; 
     private static final String ORDERS_ORDER_DATE_DESCRIPTION = 
        "OrderTO's Order Date"; 

     private static final String ORDERS_CUSTOMER_DESCRIPTION = 
           "OrderTO's Customer"; 
     private static final String ORDERS_COST_DESCRIPTION = 

     "OrderTO's Cost";  

    } 

pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
    --------- 
    <groupId>sprRestDocs</groupId> 
    <artifactId>sprRestDocs</artifactId> 
    <version>1.0.0</version> 
    <packaging>war</packaging> 

    <name>sprRestDocs</name> 
    <url>http://maven.apache.org</url> 
    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.6.RELEASE</version> 
     <relativePath/> 
    </parent> 


    <properties> 
     <java.version>1.8</java.version> 
     <springframework.version>4.3.0.RELEASE 
     </springframework.version> 
     <jackson.library>2.8.0</jackson.library> 

     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <snippetsDirectory>target/generated-snippets 
     </snippetsDirectory> 
     </properties> 

     <dependencies> 
      <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-hateoas</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-test</artifactId> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-test</artifactId> 
      <version>${springframework.version}</version>     
     </dependency> 

     <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.1.0</version> 
     <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>javax.servlet.jsp-api</artifactId> 
      <version>2.3.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
      <version>${jackson.library}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.dataformat</groupId> 
      <artifactId>jackson-dataformat-xml</artifactId> 
      <version>${jackson.library}</version> 
      </dependency>  
      <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
       <artifactId>jackson-core</artifactId> 
       <version>${jackson.library}</version> 
      </dependency>  
      <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-annotations</artifactId> 
       <version>${jackson.library}</version> 
      </dependency> 

      <dependency> 
       <groupId>com.jayway.jsonpath</groupId> 
       <artifactId>json-path</artifactId> 
       <scope>test</scope> 
       </dependency> 

       <dependency> 
       <groupId>org.springframework.restdocs</groupId> 
       <artifactId>spring-restdocs-mockmvc</artifactId> 
       <version>1.1.0.RELEASE</version> 
       <scope>test</scope> 
       </dependency> 

       <dependency> 
       <groupId>org.springframework.restdocs</groupId> 
       <artifactId>spring-restdocs-core</artifactId> 
       <version>1.1.0.RELEASE</version> 
       <scope>test</scope> 
       </dependency> 
       <dependency> 
       <groupId>junit</groupId> 
       <artifactId>junit</artifactId> 
       <version>4.12</version> 
       <scope>test</scope> 
       </dependency>  
      </dependencies> 
     <build> 
      <finalName>sprRestDocs</finalName> 
      <plugins> 
       <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
      <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.2</version> 
      <configuration> 
       <source>${java.version}</source> 
       <target>${java.version}</target> 
      </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.4</version> 
       <configuration> 
        <warSourceDirectory>src/main/webapp</warSourceDirectory> 
        <warName>sprRestDocs</warName> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.19.1</version>    
       <configuration> 
       <includes> 
        <include>>**/*Test.java</include> 
       </includes> 
       </configuration> 
      </plugin> 
      <plugin> 
      <groupId>org.asciidoctor</groupId> 
      <artifactId>asciidoctor-maven-plugin</artifactId> 
      <version>1.5.3</version> 
      <executions> 
       <execution> 
       <id>generate-docs</id> 
       <phase>prepare-package</phase> 
       <goals> 
        <goal>process-asciidoc</goal> 
       </goals> 
       <configuration> 
        <backend>html</backend> 
        <doctype>book</doctype> 
       <attributes> 
        <snippets>${snippetsDirectory}</snippets> 
       </attributes> 
      </configuration> 
      </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <artifactId>maven-resources-plugin</artifactId> 
      <version>2.7</version> 
     <executions> 
     <execution> 
      <id>copy-resources</id> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>copy-resources</goal> 
      </goals> 
      <configuration> 
      <outputDirectory> 
       ${project.build.outputDirectory}/static/docs 
      </outputDirectory> 
       <resources> 
       <resource> 
        <directory> 
         ${project.build.directory}/generated-docs 
        </directory> 
       </resource> 
       </resources> 
      </configuration> 
      </execution> 
      </executions> 
      </plugin> 
     </plugins> 
     </build> 

     <repositories> 
     <repository> 
      <id>spring-snapshots</id> 
     <name>Spring snapshots</name> 
     <url>https://repo.spring.io/libs-snapshot</url> 
     <snapshots> 
      <enabled>true</enabled> 
     </snapshots> 
     </repository> 
     </repositories> 
    </project> 

Wenn ich mvn spring-boot:run keine Dokumentation verwenden generiert.

Wenn ich mvn integration-test verwende ich bekommen api-guide.adoc [html-Datei], aber keine Schnipsel

Wenn ich versuche, mvn -Dtest=com.htc.spring.rest.docs.ForRestDocumentationTest test ich diesen Fehler zu laufen:

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 
Running com.htc.spring.rest.docs.ForRestDocumentationTest 
17:33:12.463 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.htc.spring.rest.docs.ForRestDocumentationTest] 
17:33:12.463 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 
17:33:12.479 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)] 
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.383 sec <<< FAILURE! - in com.htc.spring.rest.docs.ForRestDocumentationTest 
initializationError(com.htc.spring.rest.docs.ForRestDocumentationTest) Time elapsed: 0 sec <<< ERROR! 
java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available. 
Caused by: java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findAllMergedAnnotations(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Ljava/util/Set; 
+2

Sie haben einen 'NoSuchMethodError', der häufig auf Mischversionen zurückzuführen ist. Ich würde wetten, dass die Ursache des Fehlers Ihre Deklaration des "Federtests" mit 4.3.0 ist. Betrachtet man den Stiefel 1.3.6 pom, so erscheint Feder 4.2.7. Ich würde nur die Version in der 'Spring-Test' Abhängigkeit entfernen, so dass Sie von dem Elternteil erben. Sofern Sie nicht speziell 4.3.0 benötigen, können Sie versuchen, die Eigenschaft '' in 4.3.0.RELEASE zu ändern, wodurch die übergeordnete Version überschrieben wird. Nicht sicher, ob es funktioniert oder nicht, ich habe es nicht getestet. –

+0

Oder Sie können einfach den "Frühlingstest" entfernen. Der Teststarter zieht es schon ein. –

+0

Ich habe die Version geändert, dann hieß Test und Integrationstest funktionierte für mich, vielen Dank – raja

Antwort

0

Hallo, ich war mit Blick auf das gleiche Problem selbst, fand ich eine Lösung so das Schnipsel erstellt werden, indem Sie von

<phase>prepare-package</phase> 

zu

<phase>generate-resources</phase> 

aber immer noch ein Problem gegenüber, die erstellten Schnipsel werden immer nach HTML erzeugt wird immer, deshalb ist es nicht als Teil von HTML enthalten.

Obwohl ich in diesem Teil bin noch auf der Suche, denn jetzt eine vorübergehende Lösung dieses Problems gibt es, zuerst ausführen ich mvn clean test dann Schnipsel einmal erzeugt erhalten wieder mvn test und richtige generierten HTML-doc get execute. Dies ist keine saubere Lösung, und ich suche immer noch nach einer besseren Lösung für dieses Problem, aber es wird die Arbeit für jetzt erledigt.