Java 单元测试球衣 Restful Services
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21413738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unit testing jersey Restful Services
提问by Unknown
I'm new to unit testing and I want to test some jersey services in a project. We are using Junit. Please guide me to write test cases in better way.
我是单元测试的新手,我想在一个项目中测试一些 jersey 服务。我们正在使用 Junit。请指导我以更好的方式编写测试用例。
CODE:
代码:
@GET
@Path("/getProducts/{companyID}/{companyName}/{date}")
@Produces(MediaType.APPLICATION_JSON)
public Object getProducts(@PathParam("companyID") final int companyID,
@PathParam("date") final String date, @PathParam("companyName") final String companyName)
throws IOException {
return productService.getProducts(companyID, companyName, date);
}
Above mentioned service is working fine and I want to write junit test case to test above mentioned method. Above method will retrieve list of products (List<Product>
) in JSON format. I would like to write test case to check response status and json format.
上述服务工作正常,我想编写junit测试用例来测试上述方法。上述方法将检索List<Product>
JSON 格式的产品列表 ( )。我想编写测试用例来检查响应状态和 json 格式。
NOTE:We are using Jersey 1.17.1 version.
注意:我们使用的是 Jersey 1.17.1 版本。
Help would be appreciated :)
帮助将不胜感激:)
采纳答案by emgsilva
For Jersey web services testing there are several testing frameworks, namely: Jersey Test Framework (already mentioned in other answer - see here documentation for version 1.17 here: https://jersey.java.net/documentation/1.17/test-framework.html) and REST-Assured (https://code.google.com/p/rest-assured) - see here a comparison/setup of both (http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/).
对于 Jersey 网络服务测试,有几个测试框架,即: Jersey 测试框架(已在其他答案中提到 - 请参阅此处的 1.17 版文档:https: //jersey.java.net/documentation/1.17/test-framework.html) 和 REST-Assured ( https://code.google.com/p/rest-assured) - 在这里查看两者的比较/设置 ( http://www.hascode.com/2011/09/rest-assured- vs-jersey-test-framework-testing-your-restful-web-services/)。
I find the REST-Assured more interesting and powerful, but Jersey Test Framework is very easy to use too. In REST-Assured to write a test case "to check response status and json format" you could write the following test (very much like you do in jUnit):
我发现 REST-Assured 更有趣、更强大,但 Jersey 测试框架也非常易于使用。在 REST-Assured 中编写测试用例“检查响应状态和 json 格式”,您可以编写以下测试(非常类似于您在 jUnit 中所做的):
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");
}
}
This should do the trick for you... you can verify JSON specific elements also very easily and many other details. For instructions on more features you can check the very good guide from REST-Assured (https://code.google.com/p/rest-assured/wiki/Usage). Another good tutorial is this one http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/.
这应该可以为您解决问题……您也可以非常轻松地验证 JSON 特定元素和许多其他细节。有关更多功能的说明,您可以查看来自 REST-Assured ( https://code.google.com/p/rest-assured/wiki/Usage)的非常好的指南。另一个很好的教程是http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/。
HTH.
哈。
回答by Neeraj Krishna
Just ignore the annotations and write a normal unit test that passes the required parameters. The return I thought would usually be of type "javax.ws.rs.core.Response" ... There is a getEntity() method on that can be used. Using a Mock object framework like Mockito could be helpful in this case too.
只需忽略注释并编写通过所需参数的正常单元测试。我认为返回的通常是“javax.ws.rs.core.Response”类型......可以使用 getEntity() 方法。在这种情况下,使用像 Mockito 这样的 Mock 对象框架也可能会有所帮助。
回答by MariuszS
Are you familiar with Chapter 26. Jersey Test Framework?
你熟悉第 26 章 Jersey 测试框架吗?
public class SimpleTest extends JerseyTest {
@Path("hello")
public static class HelloResource {
@GET
public String getHello() {
return "Hello World!";
}
}
@Override
protected Application configure() {
return new ResourceConfig(HelloResource.class);
}
@Test
public void test() {
final String hello = target("hello").request().get(String.class);
assertEquals("Hello World!", hello);
}
}