25

I'm testing a void method that happens to call several other void methods in a class (all of these methods are in the same class). The method is something along these lines...

public void methodToTest() {
   methodA();
   methodB();
}

void methodA() {
   methodA1();
   methodA2();
   methodA3();
}

What I'd like to do is cause methodA() above to do nothing. That is, I want methodA() to basically be like this:

void methodA() { }

I've tried both doThrow() and doAnswer() on methodA() to no avail. It's as if those are both being completely ignored.

An example of what I've tried...

doThrow(new RuntimeException()).when(mockedClass).methodA();

Is there a way to do this just using Mockito? I'm not at liberty to change the class that's being modified.

8
  • 1
    This should work, are you sure the methodA is not final ?
    – bric3
    Oct 31, 2012 at 18:36
  • methodA is not final Oct 31, 2012 at 18:38
  • 1
    looking back at your snippet looks like you want to do a partial mock, maybe only executing methodB, while you are executing methodA. You are most certainly using mock(YourObject.class), then of course a mock does nothing, and certainly will never execute real code. You can achieve what you want a spy(YourObject.class) it will execute the real code by default. However partial mocks is recognized as test smell, if not a design smell, you should look for opportunities to make the behavior of methodA as a dependency that you could expose in your API as a AStrategy for example.
    – bric3
    Oct 31, 2012 at 19:10
  • maybe you understood the implication in the previous question, but the class itself cannot be final, or the method is final even if the modifier is not explicitly declared on the method. So the class is not final either? Oct 31, 2012 at 22:40
  • 1
    One point that has not been pointed out thus far is that in general it is not considered good practice to mock the class under test. If you need a test where the methodA has no side effect, mock the objects that methodA calls to achieve the desired result.
    – John B
    Nov 1, 2012 at 10:57

3 Answers 3

31

If you are looking to have methodB still execute, while methodA does nothing. This will do that:

TestClass spy = spy(new TestClass());
doNothing().when(spy).methodA();
spy.methodToTest(); 
6
  • I didn't mention it above, but I actually am using a spy as you listed in your example. I've attempted this exact code in my class and the internals of methodA() are still being executed which is what I'm trying to avoid... Nov 1, 2012 at 12:32
  • 1
    are you using the inverted notation of doNothing().when() like above, or are you using the when().thenReturn() notation? The latter form actually invokes the real method on a Spy when the when line of code is being invoked (i.e., when the alternate behavior of the partial mock is being altered), and that can cause problems. Hence the need to use the former doNothing notation on spies to avoid that problem Nov 2, 2012 at 22:02
  • @KevinWelker - I'm using doNothing().when() Nov 5, 2012 at 13:35
  • 1
    I did a quick test before I wrote the post and it worked the way I described. When does methodA get called? On the doNothing() line or on the methodToTest line? Are there any modifiers on the method signature on methodA?
    – Chris D
    Nov 7, 2012 at 19:41
  • For private methods check that answer
    – Enigo
    Jul 16, 2016 at 15:28
4
 @Mock
 ApplicationClass applicationClass; 


 Mockito.doNothing().when(applicationClass).myMethod(Matchers.any(Object.class));
3
  • 2
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
    – Ethan
    Jul 23, 2018 at 9:30
  • It does not, @Ethan.
    – f1v3
    Jul 29, 2022 at 13:31
  • yes it does @f1v3
    – FARS
    Mar 23 at 21:04
0

Using a spy is only going to help you if you have a return type. You can use a mock of your class and doCallRealMethod to get the desired effect.

public void methodToTestTest() {
    MockedClass mockedClass = mock(MockedClass.class);
    doCallRealMethod().when(mockedClass).methodToTest();

    mockedClass.methodToTest();

    verify(mockedClass).methodA();
    verify(mockedClass).methodB();
    verify(mockedClass, never()).methodA1();
    verify(mockedClass, never()).methodA2();
    verify(mockedClass, never()).methodA3();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.