//My Test Class
@Before public void setup() { reset(mockOpenCaseService); mockMvc = MockMvcBuilders.standaloneSetup(openCaseController).build(); 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 'value' 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 Controller method
@RequestMapping(value = "/cases/openCases.htm", method = RequestMethod.GET) public @ResponseBody DataTablesResponse<OpenCaseDto> getOpenCases(Model model, HttpServletRequest request) { ......
try { openCases = openCaseService.getOpenCases(providerId, firmId, page, pageLength, columnSortInfo); //this return empty openCases
}
....
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/d/optout.
List<ColumnOrder> columnsDetail = extractSortOrderParam(request);
List<ColumnSortInfo> columnSortInfo = createSortInfo(columnsDetail);ColumnSortInfo sortInfo = new ColumnSortInfo();
sortInfo.setColumnName("CASE_OPEN_DATE");
sortInfo.setColumnSortOrder("desc");
columnSortInfo.add(sortInfo);ColumnSortInfo I have to write .equal() and hashCode() method?@Mock List<ColumnSortInfo> columnSortInfo;List<ColumnSortInfo> columnSortInfo = new ArrayList<ColumnSortInfo>();Well, Mockito returns the setup value only when it can verify that all parameters are equal to the ones in the setup.So, it will actually call the .equals() method on the following values: providerId, firmId, page, pageLength, and columnSortInfo (of course, if the pointers are the same, then it works too).It is not clear from your snippet if that would work for each of the parameter. Try providing a valid equals() method for each of the types involved and let us know how it goes.
On 22 August 2014 05:23, Anjib Mulepati <anji...@gmail.com> wrote:
//My Test Class
@Beforepublicvoidsetup() {reset(mockOpenCaseService);mockMvc = MockMvcBuilders.standaloneSetup(openCaseController).build();List<OpenCaseDto> value =newArrayList<OpenCaseDto>();OpenCaseDto openCase =newOpenCaseDto();openCase.setResultCount(20L);value.add(openCase);when(mockOpenCaseService.getOpenCases(providerId, firmId, page, pageLength, columnSortInfo)).thenReturn(value);// this 'value' has one element
}
@TestpublicvoidgetOpenCases()throwsException {mockMvc.perform(get("/cases/openCases.htm").param("start","0")
...<code style="font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important;line-height:1.1em!important;border-top-left-radius:0px!important;border-top-right-radius:0px!important;border-bottom-right-radius:0px!important;border-bottom-left-radius:0px!important;float:none!important;min-height:auto!important;out
1) is there a way to tell mockito to mock columnSortInfo
and pass to getOpenCases(0 method and return openCases as real data doesn't matter in this case?
2) If not then is that inColumnSortInfo I have to write .equal() and hashCode()method?
when(mockOpenCaseService.getOpenCasesany(any(Long.class), any(Long.class), any(Integer.class), any(Integer.class), any(List.class)).thenReturn(openCases));List<ColumnSortInfo> columnSortInfo = createSortInfo(columnsDetail);
openCases = openCaseService.getOpenCases(providerId, firmId, page, pageLength, columnSortInfo); --
--
You received this message because you are subscribed to a topic in the Google Groups "mockito" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mockito/pPu-KOMtMHo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mockito+u...@googlegroups.com.
What error are you getting? Is it a compile error or a runtime error?
--
--
You received this message because you are subscribed to a topic in the Google Groups "mockito" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mockito/pPu-KOMtMHo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mockito+u...@googlegroups.com.
Hi
Most likely it could be issue concerning threads. Mockito operates on ThreadLocal and most likely your mocked service looses the stubbing info when a request arrives. Thats a hypothesis though.
Btw why and are you mocking a service that is your Spring bean?
Maybe i've misunderstood some of your code cause I just took a glance and didn't have time to look at what you're writing thouroughly but if you're unit testing then why do you need Spring context set up?
If you're unit testing then why do you even need a mockMvc since you could just instantiate your controller, inject mocks and execute the method that you're testing?
Can you please show your whole test class?
Also could you exactly state what's the purpose of this test and what do you want to achieve?
Would it also be able to see your sut? I'm asking cause your test seems to be really complex so either you've made your test overcomplicated or maybe your controller does too much?
Typically, a “unit test” of a service controller would not use HTTP to contact the controller. It would simply instantiate the controller and inject dependencies (Marcin basically said the same thing). What you have here is more of an integration test, as you’re really testing the REST infrastructure, not just your business logic. This is a good thing to do, but it’s not a unit test, and you shouldn’t run it as a unit test. It will run much slower than a unit test, and as unit tests should run as part of your regular build, that will be a drain on developers.
If you were to write a true unit test for your controller, you would look at the business logic of the controller and determine what in it is “critical business logic” and write your test to verify that functionality. In my experience, REST service controllers typically don’t have much logic in them, as they usually delegate most of their work to the transactional service layer. If that’s the case for you, I wouldn’t bother too much with unit tests for that layer.
I totally agree with David.
If I was to start implementing this solution via tdd I would start with writing a smoke integration fest of a controller that would return a simple OK response. Then I would start the red green reactor approach.
Next I would start writing test cases bit by bit of my functionality and write unit tests for the created collaborators where it makes sense.
Hi Russell
I'm really happy that my book helped you with your work :)
--
@Controllerpublic 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; }--
You received this message because you are subscribed to a topic in the Google Groups "mockito" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mockito/pPu-KOMtMHo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mockito+u...@googlegroups.com.