代码中 'if' 条件的 Junit 测试用例
Junit test case for 'if' condition in code
如果下面的条件 method.Here 是我的方法和我正在工作的 junit 测试 class,我会收到空指针异常。
方法
@Override
public Event getDisplayEventDetails(String ceccid,
String ceocid) {
Event evnt = null;
if(!(validate.isStringBlank(ceccid)))
{
if(!(validate.isStringBlank(ceocid)))
{
String dispEventUri = eventServicesUrl;
eventSrvcLogger.debug("dispEventUri..."+dispEventUri);
try {
ResponseEntity<Event> responseEntity = restTemplate.getForEntity(dispEventUri , Event.class);
evnt=responseEntity.getBody();
if(responseEntity.getStatusCode().toString().equals("200")){
if(evnt.getValue().length > 0){
for(int i=0;i<evnt.getValue().length;i++){
DisplayValue val = new DisplayValue();
val = evnt.getValue()[i];
eventSrvcLogger.debug(val.toString());
}
} else{
evnt.setStatusCode(responseEntity.getStatusCode().toString());
evnt.setStatus(Boolean.FALSE);
evnt.setMessage("Exception occured in handling the request BAD REQUEST");
}
}
} catch (RestClientException e) {
eventSrvcLogger.error("DisplayEventServiceImpl displayEventDetail() RestClientException",
e);
}
}
}
return evnt;
}
6月class
@Test
public void testGetDisplayEventDetails() throws Exception{
//test setup with mocking, expectations, data population
String eventServicesUrl = "http://restUrl";
Event evnt = newEvent();
ResponseEntity<Event> responseEntity = new ResponseEntity<Event>(evnt, HttpStatus.OK);
DisplayValue[] dv = new DisplayValue[1];
DisplayValue dvalue = new DisplayValue();
dvalue.setFirst_name("Ron");
dv[0] =dvalue;
evnt.setValue(dv);
new NonStrictExpectations() {
{
restTemplate.getForEntity(anyString,evnt.class );returns(responseEntity);
}
};
EventService evntSrvcImpl = new EventServiceImpl();
ReflectionTestUtils.setField(evntSrvcImpl,"eventServicesUrl", eventServicesUrl);
ReflectionTestUtils.setField(evntSrvcImpl,"restTemplate", restTemplate);
}
//execute your test case
Event evnt1 = evntSrvcImpl.getDisplayEventDetails("ceccid", "ceocid");
//perform verifications and assertions
assertNotNull(evnt);
assertEquals(evnt.getValue()[0].getName(), evnt1.getValue()[0].getName());
}
关于调试它抛出的空指针异常
在这行代码
if(responseEntity.getStatusCode().toString().equals("200"))
如何在 junit 测试中设置该值 class?
我通过在 junit 测试中添加以下行得到它 class:
HttpStatus statusCode= HttpStatus.OK;
,
responseEntity.getStatusCode(); returns(statusCode);
如果下面的条件 method.Here 是我的方法和我正在工作的 junit 测试 class,我会收到空指针异常。 方法
@Override
public Event getDisplayEventDetails(String ceccid,
String ceocid) {
Event evnt = null;
if(!(validate.isStringBlank(ceccid)))
{
if(!(validate.isStringBlank(ceocid)))
{
String dispEventUri = eventServicesUrl;
eventSrvcLogger.debug("dispEventUri..."+dispEventUri);
try {
ResponseEntity<Event> responseEntity = restTemplate.getForEntity(dispEventUri , Event.class);
evnt=responseEntity.getBody();
if(responseEntity.getStatusCode().toString().equals("200")){
if(evnt.getValue().length > 0){
for(int i=0;i<evnt.getValue().length;i++){
DisplayValue val = new DisplayValue();
val = evnt.getValue()[i];
eventSrvcLogger.debug(val.toString());
}
} else{
evnt.setStatusCode(responseEntity.getStatusCode().toString());
evnt.setStatus(Boolean.FALSE);
evnt.setMessage("Exception occured in handling the request BAD REQUEST");
}
}
} catch (RestClientException e) {
eventSrvcLogger.error("DisplayEventServiceImpl displayEventDetail() RestClientException",
e);
}
}
}
return evnt;
}
6月class
@Test
public void testGetDisplayEventDetails() throws Exception{
//test setup with mocking, expectations, data population
String eventServicesUrl = "http://restUrl";
Event evnt = newEvent();
ResponseEntity<Event> responseEntity = new ResponseEntity<Event>(evnt, HttpStatus.OK);
DisplayValue[] dv = new DisplayValue[1];
DisplayValue dvalue = new DisplayValue();
dvalue.setFirst_name("Ron");
dv[0] =dvalue;
evnt.setValue(dv);
new NonStrictExpectations() {
{
restTemplate.getForEntity(anyString,evnt.class );returns(responseEntity);
}
};
EventService evntSrvcImpl = new EventServiceImpl();
ReflectionTestUtils.setField(evntSrvcImpl,"eventServicesUrl", eventServicesUrl);
ReflectionTestUtils.setField(evntSrvcImpl,"restTemplate", restTemplate);
}
//execute your test case
Event evnt1 = evntSrvcImpl.getDisplayEventDetails("ceccid", "ceocid");
//perform verifications and assertions
assertNotNull(evnt);
assertEquals(evnt.getValue()[0].getName(), evnt1.getValue()[0].getName());
}
关于调试它抛出的空指针异常 在这行代码 if(responseEntity.getStatusCode().toString().equals("200"))
如何在 junit 测试中设置该值 class?
我通过在 junit 测试中添加以下行得到它 class:
HttpStatus statusCode= HttpStatus.OK;
,
responseEntity.getStatusCode(); returns(statusCode);