Java 用于'的Junit测试用例;如果';代码中的条件
在下面的方法中,我在if条件下得到空指针异常。这是我的方法和我正在使用的junit测试类。 方法Java 用于'的Junit测试用例;如果';代码中的条件,java,junit,jmockit,Java,Junit,Jmockit,在下面的方法中,我在if条件下得到空指针异常。这是我的方法和我正在使用的junit测试类。 方法 @覆盖 公共事件getDisplayEventDetails(字符串ceccid, 字符串(ceocid){ 事件evnt=null; 如果(!(validate.isStringBlank(ceccid))) { 如果(!(validate.isStringBlank(ceocid))) { 字符串:Venturi=eventServicesUrl; 调试(“dispEventUri…”+d
@覆盖
公共事件getDisplayEventDetails(字符串ceccid,
字符串(ceocid){
事件evnt=null;
如果(!(validate.isStringBlank(ceccid)))
{
如果(!(validate.isStringBlank(ceocid)))
{
字符串:Venturi=eventServicesUrl;
调试(“dispEventUri…”+dispEventUri);
试一试{
ResponseEntity ResponseEntity=restemplate.getForEntity(dispEventUri,Event.class);
evnt=responseEntity.getBody();
if(responseEntity.getStatusCode().toString().equals(“200”)){
如果(evnt.getValue().length>0){
对于(inti=0;i我通过向junit测试类添加以下行获得了它:
HttpStatus statusCode=HttpStatus.OK;
,
responseEntity.getStatusCode();returns(statusCode);
我通过在junit测试类中添加以下行获得它:httpStatusCode=HttpStatus.OK;
,responseEntity.getStatusCode();returns(statusCode);
@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;
}
@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());
}