0

我的测试班是

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class Employee extends BaseClass{
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
private RequestSpecification spec;
@Before
public void setUp() {
this.spec = new RequestSpecBuilder().addFilter(
        documentationConfiguration(this.restDocumentation)) 
        .build();
}
@Test
public void validateEmployee(){


    given(this.spec).accept(ContentType.JSON)
    .filter(document("index"))
    .when()
    .get("http://localhost:8080/employee/getEmployee")
    .then()
    .assertThat()
    .statusCode(is(200));
    //System.out.println(response.asString());

}
}

下面是我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>SpringApiTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringApiTest</name>
 <url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
<springframework.version>4.3.0.RELEASE
 </springframework.version>
   <jackson.library>2.8.0</jackson.library>
<snippetsDirectory>target/generated-snippets
    </snippetsDirectory>
    <spring-restdocs.version>2.0.0.BUILD-SNAPSHOT</spring-restdocs.version>
 </properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
</parent>
 <dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>3.0.0</version>
        <scope>test</scope>
    </dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
 </dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>                
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
    </dependency>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-restassured</artifactId>
        </dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <scope>test</scope>
</dependency>
  </dependencies>
<build>
 <plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

        </plugin>




    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*Documentation.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>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.restdocs</groupId>
                    <artifactId>spring-restdocs-asciidoctor</artifactId>
                    <version>${spring-restdocs.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>generate-resources</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>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring snapshots</name>
        <url>https://repo.spring.io/libs-snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

以下是面临的错误

io/console not supported; tty will not be manipulated
asciidoctor: WARNING: index.adoc: line 6: include file not found: C:/Users/HAP1PI/workspace2/SpringApiTest/src/main/asciidoc/target/generated-snippets/home/http-request.adoc
asciidoctor: WARNING: index.adoc: line 9: include file not found: C:/Users/HAP1PI/workspace2/SpringApiTest/src/main/asciidoc/target/generated-snippets/home/http-response.adoc

分享 index.adoc

= Getting Started With Spring REST Docs
This is an example output for a service running at http://localhost:8080:
.request
include::{snippets}/home/http-request.adoc[]
   .response
include::{snippets}/home/http-response.adoc[]
As you can see the format is very simple, and in fact you always get the same message.

我是 RestAssured 和 RestDocs 的新手,请帮帮我。该代码非常基本,但仍然面临问题。我不明白为什么不生成要自动生成的片段。请帮我。

4

0 回答 0