简体   繁体   English

我需要为Jersey Rest Web服务编写Junit测试用例..请帮助我或发布示例代码?

[英]I need to write Junit Test cases for Jersey Rest web service ..Please help me or post a sample code?

This is the code for which I am looking to write Junit test cases-- 这是我要为其编写Junit测试用例的代码-

@GET 

@Path("/get")

@Produces("application/json")

public User getUser() 
{

    User user= new com.rest.rahul.User();

    user.setEmpid("12");
    user.setEmail("DJ@gmail.com");
    user.setName("DJ");
    return user;

}

What do you want to unit test? 您要对什么进行单元测试? I wouldn't bother with unit testing simply setters. 我不会为简单的二传手而烦恼。 But if you want to test the JSON output, you're actually looking for something like an an integration test. 但是,如果要测试JSON输出,则实际上是在寻找集成测试之类的东西。 A test that bootstraps your application, sets up the webservice and then calls the getUser method. 引导您的应用程序的测试,设置Web服务,然后调用getUser方法。 Take a look at https://blog.codecentric.de/en/2012/05/writing-lightweight-rest-integration-tests-with-the-jersey-test-framework/ 看看https://blog.codecentric.de/en/2012/05/writing-lightweight-rest-integration-tests-with-the-jersey-test-framework/

Testing a web service with JUnit involves starting up the web service inside the test environment, performing the request, validating the response and then shutting it down. 使用JUnit测试Web服务包括在测试环境中启动Web服务,执行请求,验证响应然后关闭它。

You can use Jersey's REST Client to perform a request on a server that comes up when the test runs. 您可以使用Jersey的REST客户端在测试运行时出现的服务器上执行请求。 Get the response and Assert your required conditions on the response object. 获取响应,并在响应对象上声明所需条件。

More on Jersey's client here: 有关泽西岛客户的更多信息,请点击此处:

http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/ http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM