1

I am new to unit testing in java so please excuse if this is a silly question. I have a method like the one below. What I want to do is verify that this method is being called and also check that the parameters that are passed are not null and also being called. How can I go about doing this in junit/mockito?

public <return type> callMe(Object objectA) { if(objectA.name != null && objectA.age != null) { someOtherMethod(objectA.name, objectA.age) } } 
2
  • Does calling someOtherMethod produce any effect that can be observed from outside the class under test? If yes - then you should be asserting on that publicly visible effect, rather than the fact that someOtherMethod was called, which is an implementation detail. Commented Jun 23, 2020 at 0:29
  • So do I need to make a CallMe callMe = new CallMe and then do something like callMe.someOtherMethod ? Commented Jun 23, 2020 at 0:31

1 Answer 1

1

I think one alternative is using @Spy annotation to execute the method inside your first method and after that check the result. https://www.baeldung.com/mockito-spy

You can also use the Mockito.veify(...) to check if the method was called. https://www.baeldung.com/mockito-verify

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.