[英]Best way to unit test JAX-RS Controller?
我正在学习javax.ws.rs
库,所以我写了一个玩具API。
例如,我已经使该POST
方法起作用。 我已经看到有很多库需要测试,例如Jersey或Rest Assured,但是,如何对响应代码为200
且内容为"hello"
单元测试?
@POST
@Path("/getFoo")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Foo getFoo(@BeanParam final Foo foo)
{
return new Foo("hello");
}
对于Jersey Web服务测试,有几个测试框架,分别是:Jersey测试框架( https://jersey.java.net/documentation/1.17/test-framework.html )和REST-Assured( https://code.google.com)。 com / p / rest-assured )-参见此处两者的比较/设置( http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-网络服务/ )。
package com.example.rest;
import static com.jayway.restassured.RestAssured.expect;
import groovyx.net.http.ContentType;
import org.junit.Before;
import org.junit.Test;
import com.jayway.restassured.RestAssured;
public class Products{
@Before
public void setUp(){
RestAssured.basePath = "http://localhost:8080";
}
@Test
public void testGetProducts(){
expect().statusCode(200).contentType(ContentType.JSON).when()
.get("/getProducts/companyid/companyname/12345088723");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.