4

I'm writing a JUnit test and also using Mockito, and I want to call a method, which in turn calls a second method a number of times. I do not want that second method to ever be called during my unit test, but I want to know what it's arguments would have been. My code to be tested looks something like this:

public class MyClass {
    public void myMethod() {
        int a = [do some logic]
        int b = [do some logic];
        doSomething(a, b);

        a = [do some logic];
        b = [do some logic];
        doSomething(a, b);
    }

    public void doSomething(int a, int b) {
        // code that I do not want to be executed during a unit test
    }
}

And now a unit test:

@Test
public void test() {
    MyClass myClass = new MyClass();
    myClass.myMethod();
    verify(myClass).doSomething(17, 33);
    verify(myClass).doSomething(9, 18);
}

I am new to Mockito and I don't know if it's possible to either A) prevent doSomething() from being executed and B) verify the values of the a & b arguments. I'm willing to accept answers like "Mockito cannot help you here" or "this is not something that is technically possible." If there's no way to mock this sort of thing, I may consider refactoring those [do some logic] blocks into methods that I can test directly but my code is more complex than this simple example, and I am not allowed to post the code online.

jewbix.cube
  • 485
  • 6
  • 19
  • You may find the answers to this question useful: http://stackoverflow.com/questions/1087339/using-mockito-to-test-abstract-classes – tjdett Feb 03 '12 at 01:38

2 Answers2

5

To do this, you need a Mockito partial mock.

This is untested, but I think what you want might be something along the lines of:

@Test
public void test() {
    MyClass myClass = spy(new MyClass());
    doNothing().when(myClass).doSomething(any(Integer.class), any(Integer.class));
    myClass.myMethod();
    verify(myClass).doSomething(17, 33);
    verify(myClass).doSomething(9, 18);
}

The spy sets up a partial mock, so that you can replace some methods and use some real methods. The doNothing stubs your doSomething (an interesting naming coincidence) to do nothing instead of calling the real code.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
  • Success! However I was getting a NullPointerException for a while, learned a lot about spies and stubbing while trying to eradicate it. Of all things the NPE was caused by the `any(Integer.class)`. Had to replace it with `Mockito.anyInt()` and then voila. Weird. Thank you! – jewbix.cube Feb 03 '12 at 05:37
5

Spies are ugly. If you partially mock your class under test, you easily to get confused about what is mocked and what is really tested. Mockito's Javadoc explicitly warns against partial mocking.

Better to refactor your code so you can test it cleanly. Extract doSomething() into a new class:

public class SomethingDoer {
    public void doSomething(int a, int b) {
        // code that you do not want to be executed during a unit test
    }
}

Changed MyClass:

public class MyClass {
    private final SomethingDoer somethingDoer;

    public MyClass(SomethingDoer somethingDoer) {
        this.somethingDoer = somethingDoer;
    }

    public void myMethod() {
        int a = [do some logic]
        int b = [do some logic];
        somethingDoer.doSomething(a, b);

        a = [do some logic];
        b = [do some logic];
        somethingDoer.doSomething(a, b);
    }
}

Test:

@Test
public void test() {
    SomethingDoer somethingDoer = mock(SomethingDoer.class);
    MyClass myClass = new MyClass(somethingDoer);
    myClass.myMethod();
    verify(somethingDoer).doSomething(17, 33);
    verify(somethingDoer).doSomething(9, 18);
}
Arend v. Reinersdorff
  • 4,110
  • 2
  • 36
  • 40
  • +1: Though I was happy to tell approximately how to do partial mocks, *not* doing it is good advice. – Don Roby Feb 03 '12 at 10:19
  • I ended up having to refactor `doSomething()` into a new class today anyway for a different reason. So naturally this is the solution I went with, but it was good to get a perspective on both. – jewbix.cube Feb 04 '12 at 02:07