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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With