Junit for Controller类

时间:2016-05-26 15:42:22

标签: junit controller mockito powermockito

我有控制器方法,因为我正在制作Junit,但在调用服务方法时会出现Null指针错误。我使用了power mock但仍然使用Null指针。

方法:

                 @RequestMapping(method = RequestMethod.GET, value = "/DSR.do")
public ModelAndView displayDataSourceReportPage(HttpServletRequest request,Model model) {
    log.debug(" Inside displayDataSourceReportPage method ");
    Map<String, Object> map = new HashMap<String, Object>();
    try {
        request.setAttribute(MENU_SELECTED, LABEL_MENU_SOURCEDATA);
        request.setAttribute(SUB_MENU_SELECTED, LABEL_SUBMENU_DSR);




        @SuppressWarnings("rawtypes")
        List dataSource = dataSourceReportService.listDataSourceReportByCurrentRunInd("C");
        map.put("dataSource", dataSource);

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return new ModelAndView("DataSourceReport", "model", map);
}

测试方法:

@InjectMocks       private DataSourceReportController dataSourceReportController;

@Mock
private DataSourceReportService dataSourceReportServiceImpl;


@InjectMocks
private DataSourceReportDAO dataSourceReportDAO = new DataSourceReportDAOImpl();
@Before
public void setup() {
        MockitoAnnotations.initMocks(this);
}
@Test
public void testdisplayDataSourceReportPage() throws Exception {
    PowerMockito.mockStatic(DataSourceReport.class);
    PowerMockito.mockStatic(HttpServletRequest.class);
    PowerMockito.mockStatic(Model.class);
    PowerMockito.mockStatic(DataSourceReportService.class);

    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Model model = Mockito.mock(Model.class);
    dataSourceReportServiceImpl = PowerMockito.mock(DataSourceReportService.class);

    DataSourceReport dataSourceReport = PowerMockito.mock(DataSourceReport.class);
    dataSourceReport.setCurrentRunInd("abc");
    dataSourceReport.setActualFileName("Somthing");
    dataSourceReport.setFileCountId(3);
    dataSourceReport.setFileId(4);
    dataSourceReport.setRecCount(3);
    List<DataSourceReport> list = new ArrayList<DataSourceReport>();
    list.add(dataSourceReport);
    String currentRunInd = "currentRunInd";
    Object obj =getClass();



    PowerMockito.when(dataSourceReportDAO.listDataSourceReportByCurrentRunInd(currentRunInd)).thenReturn(list);


    DataSourceReportController ctrl = new DataSourceReportController();
    ctrl.displayDataSourceReportPage(request, model);

}

在“dataSourceReportService.listDataSourceReportByCurrentRunInd(”C“)获取Null;”

2 个答案:

答案 0 :(得分:0)

你需要在测试类

中拥有它
PowerMockito.when(dataSourceReportService.listDataSourceReportByCurrentRunInd("C")).thenReturn(list);

之前打电话

ctrl.displayDataSourceReportPage(request, model);

答案 1 :(得分:0)

谢谢@Arthur Zagretdinov

我尝试了下面的代码并且有效。

            private MockMvc mockMvc;

    @Mock
    private HttpServletRequest req;

    @Mock
    private DataSourceReportService dataSourceReportServiceImpl;

    @InjectMocks
    private DataSourceReportController controller;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }
 @Before
    public void initMocks(){
        MockitoAnnotations.initMocks(this);
    }

@Test
public void testdisplayDataSourceReportPage() throws Exception {

    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Model model = Mockito.mock(Model.class);

    DataSourceReport dataSourceReport =Mockito.mock(DataSourceReport.class);;
    dataSourceReport.setCurrentRunInd("abc");
    dataSourceReport.setActualFileName("Somthing");
    dataSourceReport.setFileCountId(3);
    dataSourceReport.setFileId(4);
    dataSourceReport.setRecCount(3);
    List<DataSourceReport> list = new ArrayList<DataSourceReport>();
    list.add(dataSourceReport);
    ModelAndView modelView = controller.displayDataSourceReportPage(request, model);
    modelView.addObject(dataSourceReport);

}