测试 JAX-RS Web 服务?

测试 JAX-RS Web 服务?

问题描述:

我目前正在寻找为 JAX-RS(Java API用于 RESTful Web 服务)基于 Web 服务.

I'm currently looking for ways to create automated tests for a JAX-RS (Java API for RESTful Web Services) based web service.

我基本上需要一种方法来向它发送某些输入并验证我是否得到了预期的响应.我更喜欢通过 JUnit 来实现这一点,但我不确定如何实现.

I basically need a way to send it certain inputs and verify that I get the expected responses. I'd prefer to do this via JUnit, but I'm not sure how that can be achieved.

您使用什么方法来测试您的网络服务?

What approach do you use to test your web-services?

更新: 正如 entzik 指出的那样,将 Web 服务与业务逻辑分离使我能够对业务逻辑进行单元测试.但是,我还想测试正确的 HTTP 状态代码等.

Update: As entzik pointed out, decoupling the web service from the business logic allows me to unit test the business logic. However, I also want to test for the correct HTTP status codes etc.

Jersey 带有一个很棒的 RESTful 客户端使编写单元测试变得非常容易的 API.请参阅 Jersey 附带的示例中的单元测试.如果您对 测试用例在这里

Jersey comes with a great RESTful client API that makes writing unit tests really easy. See the unit tests in the examples that ship with Jersey. We use this approach to test the REST support in Apache Camel, if you are interested the test cases are here