如何使用Junit为jersey api编写测试用例,以测试api响应和参数?
这是我的api,我使用了Jersey和spring boot。该api运行良好,产生了正确的响应,我想编写单元测试用例,以便使用Junit检查api如何使用Junit为jersey api编写测试用例,以测试api响应和参数?,junit,jersey,spring-boot,Junit,Jersey,Spring Boot,这是我的api,我使用了Jersey和spring boot。该api运行良好,产生了正确的响应,我想编写单元测试用例,以便使用Junit检查api @POST @Path("/trip/create") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response createTrip(Trip pObjTrip, @HeaderParam(
@POST
@Path("/trip/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createTrip(Trip pObjTrip, @HeaderParam("Auth-Token") String pAuthToken) {
Response lRetVal = new Response();
appLog.info(String.format("Creating a trip with [%s]", pObjTrip));
try {
User lObjUser = validateUser(pAuthToken, lRetVal);
if(lObjUser == null) {
} else if (StringUtils.isEmpty(pObjTrip.getTripName())) {
lRetVal.setResult(ResponseIds.API_RESULT_INSUFFICIENT_INFO);
lRetVal.setMessage("All information are not provided");
} else {
lObjUser.setEnabled(true);
pObjTrip.setCreatedBy(lObjUser);
pObjTrip.setCreateTime(Calendar.getInstance());
pObjTrip.setTripStatus(Trip.TRIP_STATUS_CREATED);
m_cObjTripService.saveAndFlush(pObjTrip);
lRetVal.setResult(ResponseIds.API_RESULT_SUCCESS);
lRetVal.setValue(pObjTrip);
}
} catch (Exception ex) {
appLog.warn(ex.toString(), ex);
}
return lRetVal;
}
我在搜索后编写了测试用例,但得到了400个错误的()请求
@After(值=”http://localhost:8080/api/trphpr/trip/create")
public void testCreateTripApi()引发IOException{
多值映射=新链接的多值映射(2);
添加地图(“tripNme”、“测试行程”);
org.springframework.http.HttpHeaders lhttpheader=new org.springframework.http.HttpHeaders();
lhttpheader.add(“身份验证令牌”,“7F8B655A007DDA8F93A77FCB44DE3298EFB67615A5585B2FDC6C95B4C4C4A52282EAE619A2FD6C3F9B4E3723E3D1CB221376ED36D978F0FF31B585D8E70F4”);
lhttpheader.setContentType(MediaType.APPLICATION_JSON);
列表请检查。@Abhishek-我会通过这个泽西测试框架,但我无法得到解决方案。请检查。@Abhishek-我会通过这个泽西测试框架,但我无法得到解决方案。
@After(value = "http://localhost:8080/api/trphpr/trip/create")
public void testCreateTripApi() throws IOException{
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(2);
map.add("tripNme", "test trip");
org.springframework.http.HttpHeaders lhttpheader = new org.springframework.http.HttpHeaders();
lhttpheader.add("Auth-Token", "7f8b655a007dda8f93a77fcb44de3298efb67615a5585b2fdc6c95b4cc2c4a52282deae6119a2fd6c3f9b4e3723e3d1cb221376ed36d978f0ff31b585d8e70f4");
lhttpheader.setContentType(MediaType.APPLICATION_JSON);
List<HttpMessageConverter<?>> msgConverters = restTemplate.getMessageConverters();
msgConverters.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(msgConverters);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, lhttpheader);
Response apiResponse = (Response) restTemplate.postForObject("http://localhost:8080/api/trphpr/trip/create", request, Response.class);
assertNotNull(apiResponse);
//Asserting the response of the API.
String message = apiResponse.getMessage();
Trip ltrip = (Trip)apiResponse.getValue();
assertEquals("trip created successfully", message);
assertEquals("trip test", ltrip.getTripName());
}
}