Results 1 to 3 of 3
  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    48
    Rep Power
    0

    Default Java Test with Mockito

    I have controller
    Java Code:
    @Controller
    public class OpenCaseController {
    	@Autowired ProviderService providerService;
    	@Autowired OpenCaseService openCaseService;
    	@Autowired DomainMemberService domainMemberService;
    	
    	@RequestMapping(value = "/cases/openCases.htm", method = RequestMethod.GET)
    	public @ResponseBody DataTablesResponse<OpenCaseDto> getOpenCases(Model model, HttpServletRequest request) {
    		Long providerId = providerService.getLoggedInProvider().getId();
    		Long firmId = domainMemberService.getMember(providerId).asProvider().getFirm().getId();
    		Integer startIndex = Integer.parseInt(request.getParameter("start"));
    		Integer pageLength = Integer.parseInt(request.getParameter("length"));
    		Integer page = startIndex/pageLength + 1;
    		DataTablesResponse<OpenCaseDto> response = new DataTablesResponse<OpenCaseDto>();
    		List<OpenCaseDto> openCases = new ArrayList<OpenCaseDto>();
    		List<ColumnOrder> columnsDetail = extractSortOrderParam(request);
    		List<ColumnSortInfo> columnSortInfo = createSortInfo(columnsDetail);
    		try {
    			openCases = openCaseService.getOpenCases(providerId, firmId, page, pageLength, columnSortInfo);	//this is empty		
    			Integer totalRecordFiltered = openCases.get(0).getResultCount().intValue();
    			response.setRecordsTotal(totalRecordFiltered);
    			response.setRecordsFiltered(totalRecordFiltered);
    			response.setData(openCases);
    			response.setError(null);
    		} catch(Exception e) {
    			response.setRecordsTotal(0);
    			response.setRecordsFiltered(0);
    			response.setData(openCases);
    			response.setError("DB Error" + e.getMessage());
    		}
    		response.setDraw(Integer.parseInt(request.getParameter("draw")));
    		
    		return response;
    	}
    and I am writing test using Mockito for this controller
    Java Code:
    @RunWith(MockitoJUnitRunner.class)
    public class OpenCaseControllerTest {
    	MockMvc mockMvc;
    	
    	@Mock OpenCaseService mockOpenCaseService;
    	@Mock ProviderService mockProviderService;
    	@Mock DomainMemberService mockDomainMemberService;
    	@Mock Provider provider;
    	@Mock Individual individual;
    	@Mock ProviderFunctions providerFunctions;
    	@Mock Organization organization;
    	
    	@InjectMocks OpenCaseController openCaseController = new OpenCaseController();
    	
    	Long providerId = 12345L;
    	Long firmId = 67890L;
    	Integer page = 1;
    	Integer pageLength = 20;
    	String start = "0";
    	@Mock List<ColumnSortInfo> columnSortInfo;
    	
    	
    	@Before
    	public void setup() {
    		reset(mockProviderService, mockOpenCaseService, mockDomainMemberService);
    		mockMvc = MockMvcBuilders.standaloneSetup(openCaseController).build();
    		
    		when(mockProviderService.getLoggedInProvider()).thenReturn(provider);
    		when(provider.getId()).thenReturn(providerId);
    		
    		when(mockDomainMemberService.getMember(providerId)).thenReturn(individual);
    		when(individual.asProvider()).thenReturn(providerFunctions);
    		when(providerFunctions.getFirm()).thenReturn(organization);
    		when(organization.getId()).thenReturn(firmId);
    		
    		List<OpenCaseDto> value = new ArrayList<OpenCaseDto>();
    		OpenCaseDto openCase = new OpenCaseDto();
    		openCase.setResultCount(20L);
    		value.add(openCase);
    		
    		when(mockOpenCaseService.getOpenCases(providerId, firmId, page, pageLength, columnSortInfo)).thenReturn(value); // this one has one element
    	}
    	
    	@Test
    	public void getOpenCases() throws Exception {
    		mockMvc.perform(get("/cases/openCases.htm")
    				.param("start", "0")
    				.param("length", "20")
    				.param("draw", "0"))
    			.andExpect(status().isOk())
    			.andExpect(content().contentType(new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"))));
    	}
    
    }
    My test is failing because controller return 406 instead of 200. When I debug it shows getOpenCases() call in controller return empty list. But I have when().thenReturn() in test to return a dummy object. Can anyone tell me why its not working?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Java Test with Mockito

    Where does columnSortInfo come from?
    I can't see anything in there (and it could just be Friday morning blindness on my part) that returns it, or gets that into the executing code such that it could be passed into getOpenCases as a parameter.

    I can see where the other ones come from (I think).
    So (at a brief look) it looks to me like there is no match between the actual call to getOpenCases and the 'when' in the mock.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    48
    Rep Power
    0

    Default Re: Java Test with Mockito

    So with following change controller method does return me response but my test is failing with 406 error.
    Java Code:
    ColumnSortInfo sortInfo = new ColumnSortInfo();
    sortInfo.setColumnName("CASE_OPEN_DATE");
    sortInfo.setColumnSortOrder("desc");
    columnSortInfo.add(sortInfo);
    Mockito.doReturn(openCases).when(mockOpenCaseService).getOpenCases(providerId, firmId, page, pageLength, columnSortInfo);
    Is there something I have to do since my controller is returning as @ResponseBody?

Similar Threads

  1. Replies: 0
    Last Post: 04-05-2008, 01:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •