代码中“if”条件的Junit测试用例

时间:2016-03-04 11:26:24

标签: java junit jmockit

我在以下方法的if条件下得到空指针异常。这是我的方法和junit测试类我正在工作。 方法

@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;
}

Junit课程

@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());
}

在调试其抛出空指针异常时 在这行代码     如果(responseEntity.getStatusCode()。的toString()。等于( “200”))

如何在junit测试类中设置该值?

1 个答案:

答案 0 :(得分:0)

我通过在junit测试类中添加以下行来获得它:

HttpStatus statusCode= HttpStatus.OK;responseEntity.getStatusCode(); returns(statusCode);