gpt4 book ai didi

java - 是否可以将 Spring Restdocs 与 Jersey 应用程序一起使用

转载 作者:行者123 更新时间:2023-11-30 06:55:39 25 4
gpt4 key购买 nike

Spring Restdocs 基于 Spring MVC 测试。因此,我想弄清楚是否可以将 Spring Restdocs 与 Jersey 2.0 REST 应用程序集成。

如果是这样,能否请您指出任何相关示例或教程。

最佳答案

注意:下面的更新 2:已发布第三方库以与 Jersey 一起使用

他们目前正在研究 restassured support它与 MVC(或任何其他服务器框架)无关。我想这应该在 1.1.0 中发布。我添加了一个 feature request对于 Jersey Test Framework 支持,但我不知道他们对此有何看法。如果你想看到对它的支持,你可以在问题中发表评论。但我想他们会采取不需要 Jersey Test Framework 支持的立场,因为您仍然可以使用 REST Assured 作为 Jersey Test Framework 测试用例的客户端。但我们会看到,你永远不会知道 ;-)


更新

在这个答案发布后不久,Spring REST Docs 将 rest-assured 分支与 master 分支合并,因此您现在可以使用 spring-restdocs-restassured 的快照不需要自己构建项目的工件。您只需要添加 Spring Snapshot 存储库。下面是一个使用 Jersey 测试框架的完整示例。

import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.restdocs.RestDocumentation;

import com.fasterxml.jackson.databind.ObjectMapper;

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document;
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.documentationConfiguration;

/**
* Stack Overflow http://stackoverflow.com/q/35068860/2587435
*
* Run this like any other JUnit test. The required dependencies are listed below. You will need
* to add the Spring Snapshot repository, also listed below.
*
* Running the test should produces the following snippets in target/generated-snippets/example-put:
*
* - curl-request.adoc
* - http-request.adoc
* - http-response.adoc
* - path-parameters.adoc
* - request-fields.adoc
* - response-fields.adoc
*
* <dependencies>
* <dependency>
* <groupId>org.springframework.restdocs</groupId>
* <artifactId>spring-restdocs-restassured</artifactId>
* <version>1.1.0.BUILD-SNAPSHOT</version>
* <scope>test</scope>
* </dependency>
* <dependency>
* <groupId>org.glassfish.jersey.test-framework.providers</groupId>
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
* <version>2.22.1</version>
* <scope>test</scope>
* </dependency>
* <dependency>
* <groupId>org.glassfish.jersey.media</groupId>
* <artifactId>jersey-media-json-jackson</artifactId>
* <version>2.22.1</version>
* <scope>test</scope>
* </dependency>
* <dependency>
* <groupId>commons-logging</groupId>
* <artifactId>commons-logging</artifactId>
* <version>1.2</version>
* <scope>test</scope>
* </dependency>
* <dependency>
* <groupId>org.hamcrest</groupId>
* <artifactId>hamcrest-all</artifactId>
* <version>1.3</version>
* <scope>test</scope>
* </dependency>
* </dependencies>
*
* <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>
*
* @author Paul Samsotha
*/
public class RestAssuredDocs extends JerseyTest {

@Rule
public final RestDocumentation restDocumentation
= new RestDocumentation("target/generated-snippets");

public static class TestBean {
public int id;
public String message;
public TestBean (){}
public TestBean(int id, String message) {
this.id = id;
this.message = message;
}
}

@Path("test")
@Produces("application/json")
@Consumes("application/json")
public static class TestResource {

@PUT
@Path("{id}")
public TestBean update(TestBean bean) {
return bean;
}
}

@Override
public ResourceConfig configure() {
return new ResourceConfig(TestResource.class)
.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}

private final int port = 9998;
private final ObjectMapper mapper = new ObjectMapper();

@Test
public void examplePut() throws Exception {
TestBean bean = new TestBean(1, "a message");
given().port(this.port)
.filter(documentationConfiguration(this.restDocumentation))
.filter(document("example-put",
requestFields(
fieldWithPath("id").description("The id"),
fieldWithPath("message").description("The message")
),
responseFields(
fieldWithPath("id").description("The id"),
fieldWithPath("message").description("The message")
),
pathParameters(
parameterWithName("id").description("The id")
)
))
.contentType("application/json")
.accept("application/json")
.content(mapper.writeValueAsString(bean))
.put("/test/{id}", "1")
.then()
.statusCode(200)
.body("id", equalTo(1))
.body("message", equalTo("a message"));

}
}

有关如何使用 REST Assured 的更多示例,请访问 User Guide


更新 2

刚刚发布了 Jersey 的一个实现。您可以找到项目 here 。基本用法如下所示。查看项目中的wiki获取更多信息

依赖性

<properties>
<your.jersey.version>2.23</your.jersey.version>
<restdocsext.jersey.version>0.1.0</restdocsext.jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>io.github.restdocsext</groupId>
<artifactId>restdocsext-jersey</artifactId>
<version>${restdocsext.jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${your.jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${your.jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${your.jersey.version}</version>
</dependency>
</dependencies>

示例

// Other imports excluded for brevity
import static io.github.restdocsext.jersey.JerseyRestDocumentation.document;
import static io.github.restdocsext.jersey.JerseyRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders;

public class SimpleDocumentation extends JerseyTest {

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

@Path("test")
public static class TestResource {
@GET
public String getSimple() {
return "SimpleTesting";
}
}

@Override
public ResourceConfig configure() {
return new ResourceConfig(TestResource.class);
}

@Test
public void getSimple() {
final Response response = target("test")
.register(documentationConfiguration(this.documentation))
.register(document("get-simple",
preprocessRequest(removeHeaders("User-Agent"))))
.request()
.get();
assertThat(response.getStatus(), is(200));
assertThat(response.readEntity(String.class), is("SimpleTesting"));
}
}

关于java - 是否可以将 Spring Restdocs 与 Jersey 应用程序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35068860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com