1
저는 Mockito와 testclasses를 처음 접해 왔습니다.Mockito - 서비스를 조롱 할 때 nullpointerException을 throw합니다.
컨트롤러 용 테스트 클래스를 작성하려고합니다. 테스트를 실행할 때 Dto 객체 목록을 반환하도록 내 서비스를 모의하고 싶습니다. 하지만 이렇게하면 오류가 발생합니다.
내 코드 :
컨트롤러 클래스
@Controller
public class CalendarController {
@Resource
private CalendarService calendarService;
@RequestMapping(method = RequestMethod.GET,value = RequestMappings.CALENDAR, produces = ContentType.APPLICATION_JSON)
public ResponseEntity<List<CalendarDto>> getCalendarMonthInfo(@PathVariable final String userId, @PathVariable final String year)
{
List<CalendarDto> result = new ArrayList<CalendarDto>();
result = calendarService.getMonthInfo(userId,Integer.parseInt(year));
return new ResponseEntity<>(result, HttpStatus.OK);
}
테스트 클래스
public class CalendarControllerTest extends BaseControllerIT {
List<CalendarDto> calendarDto;
CalendarDto test1 , test2;
String userId = "20";
String year = "2014";
@Mock
public CalendarService calendarService;
@Before
public void setUp() throws Exception {
calendarDto = new ArrayList<CalendarDto>();
test1 = new CalendarDto();
test1.setStatus(TimesheetStatusEnum.APPROVED);
test1.setMonth(1);
test2 = new CalendarDto();
test2.setMonth(2);
test2.setStatus(TimesheetStatusEnum.REJECTED);
calendarDto.add(test1);
calendarDto.add(test2);
}
@Test
public void testGet_success() throws Exception {
when(calendarService.getMonthInfo(userId,Integer.parseInt(year))).thenReturn(calendarDto);
performGet(UrlHelper.getGetCalendarMonthInfo(userId,year)).andExpect(MockMvcResultMatchers.status().isOk());
}
}
내가 시험에 NullPointerException이 수 (필자는 "때"부분을 호출 할 때) . 더 자세히 살펴보면 모든 변수가 좋지만 모의 서비스가 null 인 것으로 나타났습니다.
뭔가를 인스턴스화하는 것을 잊어 버렸는지, 아니면이 작업을 수행하는 방식이 완전히 잘못된 것입니까?
당신이 줄 수있는 도움이나 조언을 환영합니다.
덕분에,이 내가 없어진 실제로 것입니다. –
고마워, 내가 필요한 정확한 것. –