Lee*_*Lee 5 java junit restlet
使用Restlet我为我的Java应用程序创建了一个路由器.
从使用curl,我知道每个不同的GET,POST和DELETE请求都适用于每个URI并返回正确的JSON响应.
我想为每个URI设置JUnit测试,以使测试过程更容易.但是,我不确定向每个URI发出请求以获取JSON响应的最佳方法,然后我可以进行比较以确保结果符合预期.有关如何做到这一点的任何想法?
您可以使用Restlet Client发出请求,然后检查每个响应及其表示.
例如:
Client client = new Client(Protocol.HTTP);
Request request = new Request(Method.GET, resourceRef);
Response response = client.handle(request);
assert response.getStatus().getCode() == 200;
assert response.isEntityAvailable();
assert response.getEntity().getMediaType().equals(MediaType.TEXT_HTML);
// Representation.getText() empties the InputStream, so we need to store the text in a variable
String text = response.getEntity().getText();
assert text.contains("search string");
assert text.contains("another search string");
Run Code Online (Sandbox Code Playgroud)
我实际上并不熟悉JUnit,assert或者一般的单元测试,所以如果我的例子中有些东西我很抱歉.希望它仍然说明了一种可能的测试方法.
祝好运!
| 归档时间: |
|
| 查看次数: |
4827 次 |
| 最近记录: |