Mohamed Ali
Mohamed Ali

Reputation: 782

How to test method call from within another method using junit

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)

  }
}

Upvotes: 1

Views: 1035

Answers (1)

Armando Almeida
Armando Almeida

Reputation: 96

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

Upvotes: 1

Related Questions