Reputation: 2049
I wrote another question previously. There I got an error executing a JUnit test on a rest service because the JSON was just a String for Jersey.
public class MachineResponseTest {
private static final String BASE_URI = "http://localhost:8080/dni-fe/home"
@Test
public void testDevice() {
WebResource resource = Client.create().resource(BASE_URI);
resource.accept(MediaType.APPLICATION_JSON);
StringBuilder sb = new StringBuilder();
sb.append("{\"name\":\"123456\",\n");
sb.append(" \"country\":\"Spain\",\n");
sb.append(" \"company\":\"xxx\",\n");
sb.append(" \"model\":\"1.10.0\"\n}");
MachineResponse result = resource.post(MachineResponse.class,sb.toString());
}
Then I tried to convert the StringBuilder into a org.codehaus.jettison.json.JSONObject
object and to return a String insted of a POJO; in this way the previous error disappeared:
try{
StringBuilder sb = new StringBuilder();
sb.append("{\"name\":\"123456\",\n");
sb.append(" \"country\":\"Spain\",\n");
sb.append(" \"company\":\"xxx\",\n");
sb.append(" \"model\":\"1.10.0\"\n}");
JSONObject jsonObj = new JSONObject(sb.toString());
}
catch(JSONException e){
....
}
String result = resource.post(String.class,jsonObj);
For example, suppose I have this POJO class:
public class ItemBean implements Serializable {
private static final long serialVersionUID = 7438046484680798158L;
private String name;
private String country;
private String company;
private String model;
public ItemBean() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
I tried to use this POJO to build my test, for example:
ItemBean item = new ItemBean();
item.setName("123456");
item.setCountry("Spain");
item.setCompany("xxxx");
item.setModel("1.10.0");
String result = resource.post(String.class,item);
but the test ended with an error:
com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class it.dni.rest.models.DeviceBean, and MIME media type, application/octet-stream, was not found at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
So, my target is to map an object in JSON and viceversa directly but Jersey is not doing it for me. What can I do to enable this mapping using Jersey in Junit test?
Upvotes: 0
Views: 2882
Reputation: 1363
Try Jersey Test for testing
Jersey Test Framework originated as an internal tool used for verifying the correct implementation of server-side components. Testing RESTful applications became a more pressing issue with "modern" approaches like test-driven development and users started to look for a tool that could help with designing and running the tests as fast as possible but with many options related to test execution environment. Current implementation of Jersey Test Framework supports the following set of features: pre-configured client to access deployed application support for multiple containers - grizzly, in-memory, jdk, simple, jetty able to run against any external container automated configurable traffic logging Jersey Test Framework is primarily based on JUnit but you can run tests using TestNG as well. It works almost out-of-the box and it is easy to integrate it within your Maven-based project. While it is usable on all environments where you can run JUnit, we support primarily the Maven-based setups.
Refer https://jersey.java.net/documentation/latest/test-framework.html
Upvotes: 0
Reputation: 806
There are few libraries that really help with unit testing REST webservices. Rest-assured: https://github.com/rest-assured/rest-assured is one and Unirest http://unirest.io/ is another. I just started using Unirest and it is super easy to use for JSON. Might be worth a look.
Upvotes: 2