2

I found my method using ReflectionUtils

Method myMethod=ReflectionUtils.findMethod(myMockClass.getClass(), "myMethod", myArg.class)

Now I would like to drive this method to return a specified value. Normally, if myMethod would be public I would write for instance

given(myMockClass.myMethod(myArg)).willReturn(5)

But is there any possibility to do it with private myMethod?
When I've called

given(myMethod.invoke(myClass, myArg)).willReturn(5)    

I've got java.lang.reflect.InvocationTargetException. I've read about PowerMock, but I would like to know whether it's possible with Mockito only

Edit:

public int A(args){
  int retValue;
  ... some code here, the most important part
  retValue=..
  if(some case)
      retValue= myMethod(args);
  return retValue;
}
entpnerd
  • 10,049
  • 8
  • 47
  • 68
adolzi
  • 671
  • 2
  • 7
  • 15
  • First of all, what class do you want to test? Class which contains 'A' method? What does the 'myMethod' do? I mean, how does the method call the another class? Does it use 'new' or static call? – Artur Zagretdinov Mar 25 '16 at 03:57

3 Answers3

2

Consider using Guava's @VisibleForTesting annotation here. Basically, just increase the visibility of the method to the minimal level necessary to test it.

For example, if your original method is:

private int calculateMyInt() {
  // do stuff
  return 0;
}

Your new method would be:

@VisibleForTesting // package-private to call from test class.
int calculateMyInt() {
  // do stuff
  return 0;
}
entpnerd
  • 10,049
  • 8
  • 47
  • 68
1

I would recommend you not to do it. If you need to mock behavior of private method then you have problems with your design. Your class is not testable.

As workaround will be to make your method package private, and test it within same package. This will work, but also not considered as good practice.

I would recommend to read this latest Uncle's bob article

Igor Konoplyanko
  • 9,176
  • 6
  • 57
  • 100
  • Thank you for your comment, I'll take it into account. My situation is following (edit), I have method A where are a few functionalities and inside this method I call myMethod in one case. myMethod is private and it invokes to another class. I would like to write unit test so I don't want to focus what is happening in this another class, because it's different tes, in this case I would like to return some value. – adolzi Mar 23 '16 at 10:39
  • Maybe I'm wrong, but let's take an example that we have one class where we do a lot of things including calling methodA where we compute some value using specific algorithm, this methodA is private, and than it's ok that we test only this algorithm – adolzi Mar 23 '16 at 10:43
1

Don't hesitate to change the method visibility to package protected, even if it's only for testing purpose (typically because you want to test the method or because you want to mock it). You should clearly indicate that fact though, a good way is by using an annotation (see this answer).

Community
  • 1
  • 1
Thomas Naskali
  • 551
  • 5
  • 19