在 Restlet 路由器上运行 JUnit 测试
我使用 Restlet 为我的 Java 应用程序创建了一个路由器。
通过使用curl,我知道每个不同的GET、POST 和POST 都是不同的。 DELETE 请求适用于每个 URI,并返回正确的 JSON 响应。
我想为每个 URI 设置 JUnit 测试,以使测试过程更容易。但是,我不确定向每个 URI 发出请求以获得 JSON 响应的最佳方式,然后我可以比较该响应以确保结果符合预期。关于如何做到这一点有什么想法吗?
Using Restlet I have created a router for my Java application.
From using curl, I know that each of the different GET, POST & DELETE requests work for each of the URIs and return the correct JSON response.
I'm wanting to set-up JUnit tests for each of the URI's to make the testing process easier. However, I'm not to sure the best way to make the request to each of the URIs in order to get the JSON response which I can then compare to make sure the results are as expected. Any thoughts on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以只使用 Restlet
Client
发出请求,然后检查每个响应及其表示。例如:
我实际上不太熟悉 JUnit、assert 或一般的单元测试,所以如果我的示例有问题,我深表歉意。希望它仍然说明了一种可能的测试方法。
祝你好运!
You could just use a Restlet
Client
to make requests, then check each response and its representation.For example:
I'm actually not that familiar with JUnit,
assert
, or unit testing in general, so I apologize if there's something off with my example. Hopefully it still illustrates a possible approach to testing.Good luck!
对 ServerResource 进行单元测试
其中路由器和资源在我的测试类中是@Autowired。 Spring应用程序上下文中的相关声明如下所示
,
myApplication
类似于Unit testing a ServerResource
where the
router
and the resource are @Autowired in my test class. The relevant declarations in the Spring application context looks likeAnd the
myApplication
is similar to我在 REST junit 测试用例中得到了质询响应设置的答案
I got the answer for challenge response settings in REST junit test case
根据Avi Flax的答案,我将此代码重写为java并使用junitparams,一个允许通过参数化测试的库。代码如下:
Based on the answer of Avi Flax i rewrite this code to java and run it with junitparams, a library that allows pass parametrized tests. The code looks like: