0

I am making a junit test for a module.

when(myDetailsFacade.getMyDetailsInfo(anyLong())).thenReturn(null);
mrc.getFlowScoe().put("action", "create");
try{
    instance.initialiseForm(mrc, mpr);
   }catch (Exception e){
      fail("Shouldn't get here");
   }
model=(MyDetailsForm)mrc.getFlowScope().get("myDetailsFormModel");
assertNotNull(model);
assertEquals(model.getNationality(), "USA");

and i am getting this error

org.mockito.excetions.misusing.MissingMethodInvocationException:
when()requires an argument which has to be a method call on a mock.
For example
when(mock.getArticles()).thenReturn(articles);

in out facade we are getting hardcode value, like.

private myDetails getMyDetailsData{
myDetails.setNationality("USA");
return myDetails;
}

this is my initialiseform method which i am trying to test.

public void initialiseForm (RequestContext requestContext, PortletRequest portletRequest){

requestContext.getFlowScope().put("getTitles", getTitles);
requestContext.getFlowScope().put("getGender", getGender);

BasicModel model = (BasicModel)requestContext.getFlowScope().get("BasicModel");
if(model == null){
model = new BasicModel();
}

model.setEmpName("Edward");
}

And here is the getTitles method in same java file.

private Map <String, String> getTitles(){

option.put("Dr", "Dr");
option.put("Mr", "Mr");
return option;

}
Akash
  • 816
  • 3
  • 13
  • 38
  • 1
    Can you post a greater portion of your code surrounding the call on the mock? – Tim Biegeleisen Aug 26 '15 at 09:16
  • How are you creating `myDetailsFacade`? – Edd Aug 26 '15 at 09:20
  • Facade is nothing we are just getting the hard code value from facade – Akash Aug 26 '15 at 09:23
  • 1
    `myDetailsFacade` should be a mock object of some sort - [See the docs](http://mockito.github.io/mockito/docs/current/org/mockito/Mockito.html#when(T)) for more info. – Edd Aug 26 '15 at 09:26
  • @Edd check the code again – Akash Aug 26 '15 at 09:27
  • 1
    possible duplicate of [Mockito Exception - when() requires an argument which has to be a method call on a mock](http://stackoverflow.com/questions/9186604/mockito-exception-when-requires-an-argument-which-has-to-be-a-method-call-on) – Edd Aug 26 '15 at 09:33

1 Answers1

0

As hinted at in the exception you've received, the when() static method is for stubbing methods on mock objects. You need myDetailsFacade to be a mock if you want to stub the result of a method call. Try something like (with the actual type of myDetailsFacade of course):

MyDetails myDetailsFacade= mock(MyDetails.class)
when(myDetailsFacade.getMyDetailsInfo(anyLong())).thenReturn(null);
Edd
  • 3,724
  • 3
  • 26
  • 33
  • I tried that but getting same error, Please check the question again i put the code snippet which i want to test. – Akash Aug 27 '15 at 06:52
  • @Akash I still don't see the place where you assign a value to `myDetailsFacade`. Can you provide the full details of the test that doesn't work. – Edd Aug 27 '15 at 08:06
  • Please take a look of my initialiseForm public method at above code i want to make a junit test for that. – Akash Aug 28 '15 at 06:24
  • And take a look of this also http://akashjaiswalliferay.blogspot.in/2015/08/test.html – Akash Aug 28 '15 at 07:33
  • @Akash I think there's already enough information about the code that's being tested - the thing that would be useful is to see the full test, not just the snippet you've provided. – Edd Aug 28 '15 at 08:21
  • i have given the link where i have provided all the code – Akash Aug 28 '15 at 08:58
  • @Akash I don't see the the `when(myDetailsFacade.getMyDetailsInfo(anyLong())).thenReturn(null);` line (i.e. the one that has the problem) anywhere there. That's what I want more context of. – Edd Aug 28 '15 at 09:00