It is perfectly normal to use Mockito that way. But if your code becomes more complex, you need to do a few more things to check your code as simple as possible.
Another small example:
public void eitherAorB() { if(somethingIsTrue) { a.doSomething(); } else { b.doSomethingElse(); } }
You might want to make sure that the expected method is called on the expected object.
@Test public doSomethingShouldBeCalledOnA() { A a = mock(A.class); C c = new C(a, new B()); c.setSomeThingIsTrue(true); eitherAorB(); verify(a).doSomething(); } @Test public doSomethingElseShouldBeCalledOnB() { B b = mock(B.class); C c = new C(new A(), b); c.setSomeThingIsTrue(false); eitherAorB(); verify(b).doSomethingElse(); }
In other cases, you may need to know what parameter was passed to the method. For this you need an ArgumentCaptor.
Or in some cases, you need to call the actual method or use the actual object (without a layout), so it's time to spy on the object, grab the arguments, or test the behavior.
Thus, for Mockito you may need a lot more time.
source share