Java 测试 RESTful 服务的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2165561/
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
Ways to test RESTful services?
提问by Daff
I want to test my RESTful applications directly via HTTP and I am looking for tools that can help me with that task. Basically I am looking for an easy wrapper for HTTP requests that can submit e.g. HTML forms or serialized resources as JSON or XML.
我想直接通过 HTTP 测试我的 RESTful 应用程序,我正在寻找可以帮助我完成这项任务的工具。基本上,我正在寻找一个简单的 HTTP 请求包装器,可以将 HTML 表单或序列化资源作为 JSON 或 XML 提交。
It would be great if there is a way to verify if the service is actually following REST architectural guidelines (statelessness, URIs, content negotiation etc.), too.
如果有一种方法可以验证服务是否真的遵循 REST 架构指南(无状态、URI、内容协商等),那就太好了。
Being able to use it with JUnit would be a convenient bonus. Do you know about any libraries that could help me with what I want to do (and that are a little more than just a simple http client)?
能够将它与 JUnit 一起使用将是一个方便的奖励。你知道任何可以帮助我完成我想做的事情的库吗(不仅仅是一个简单的 http 客户端)?
采纳答案by Vishal
See if rest-clientis of any help.
看看rest-client是否有帮助。
Edit: Currently I am using Postman - REST Client a google chrome plugin and it's awesome!
编辑:目前我正在使用 Postman - REST Client 一个谷歌浏览器插件,它很棒!
回答by Gabriel ??erbák
Maybe Selenium can be of some help, but surely not entirely.
也许硒可以提供一些帮助,但肯定不完全。
回答by Johan
I think REST Assuredwill suite you very well. It's very easy to send requests and to parse XML and JSON responses. E.g. let's say that a GET request to "/lotto" returns JSON:
我认为REST Assured非常适合您。发送请求和解析 XML 和 JSON 响应非常容易。例如,假设对“/lotto”的 GET 请求返回 JSON:
{
"lotto":{
"lottoId":5,
"winning-numbers":[2,45,34,23,7,5,3],
"winners":[{
"winnerId":23,
"numbers":[2,45,34,23,3,5]
},{
"winnerId":54,
"numbers":[52,3,12,11,18,22]
}]
}
}
You can make the request and validate the response like this:
您可以像这样发出请求并验证响应:
expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");
回答by Mat
Fiddler is a really useful tool, you can create XML based HTTP Requests with a variety of request verbs like GET,POST,PUT,DELETE and so on.
Fiddler 是一个非常有用的工具,您可以使用各种请求动词(如 GET、POST、PUT、DELETE 等)创建基于 XML 的 HTTP 请求。
回答by hascode
There is also the Jersey Test Framework (http://jersey.java.net/nonav/documentation/latest/user-guide.html#test-framework) but as Johan already mentioned the REST-assured framework I'd also recommend this framework - it has some nice featues like a DSL like syntax, XPath and Schema validation, easy file upload and using Groovy Lambda Expressions to search through returned JSON structures..
还有 Jersey 测试框架 (http://jersey.java.net/nonav/documentation/latest/user-guide.html#test-framework) 但正如 Johan 已经提到的 REST-assured 框架我也推荐这个框架 - 它有一些不错的特性,比如像语法一样的 DSL、XPath 和模式验证、简单的文件上传和使用 Groovy Lambda 表达式来搜索返回的 JSON 结构..
I have written two articles..
写了两篇文章。。
- the first one compares REST-assured and Jersey-Test-Framework (http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/),
- the second explores the features of the REST-assured framework against a given REST service (http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured-framework/)
- 第一个比较 REST-assured 和 Jersey-Test-Framework ( http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/),
- 第二部分探讨了 REST-assured 框架针对给定 REST 服务的特性(http://www.hascode.com/2011/10/testing-restful-web-services-made-easy-using-the-rest-assured -框架/)