java 使用Rest Assured和Spring Boot来休息Api文档

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用Rest Assured和Spring Boot来休息Api文档相关的知识,希望对你有一定的参考价值。

@Entity
public class Note {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;

    private String details;

    public Note() {
    }

    public Note(String title, String details) {
        this.title = title;
        this.details = details;
    }
  //... getters and setters
}
package net.entrofi.spring.restassuredasciidoctor.data.repository;

import net.entrofi.spring.restassuredasciidoctor.data.entity.Note;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource
public interface NoteRepository extends JpaRepository<Note, Long> {
}
package net.entrofi.spring.restassuredasciidoctor.data.repository;

import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import net.entrofi.spring.restassuredasciidoctor.RestAssuredAsciiDoctorApplication;
import net.entrofi.spring.restassuredasciidoctor.data.entity.Note;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;

import static io.restassured.RestAssured.given;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {RestAssuredAsciiDoctorApplication.class}, webEnvironment = RANDOM_PORT)
@AutoConfigureRestDocs
public class NoteRepositoryIntTest {
    @Value("${local.server.port}")
    private int port;

    @Value("${deployment.environment.host:http://localhost}")
    private String host;

    @Autowired
    private RequestSpecification documentationSpec;


    @Test
    public void saveNote_given_a_valid_note_request_should_return_created() {
        Note note = new Note("Documented call", "Documented call details");
        given(documentationSpec).filter(document("save-note-created"))
                .body(note).contentType(ContentType.JSON)
                .when().post(host + "/" + "notes")
                .then().statusCode(HttpStatus.CREATED.value());
    }

}

以上是关于java 使用Rest Assured和Spring Boot来休息Api文档的主要内容,如果未能解决你的问题,请参考以下文章

如何为 Rest Assured 3.0.3(使用 Java 和 TestNG)声明默认解析器?

是否可以使用 Java Stream api 重写带有 Rest-Assured 请求尝试的 WHILE 循环

使用 REST Assured 进行测试时出现 java.lang.AbstractMethodError

如何将JSON响应转换为Java List-使用Rest Assured进行API测试

rest-assured的对象映射(序列化和反序列化)

rest-assured的对象映射(Object Mapping)