Mockito : how to verify method was called on an object created within a method?

To verify that a method was called on an object created within a method using Mockito, you can use the Mockito.verify() method and pass it the object that you want to verify, as well as the method that you want to verify was called.

Here is an example of how you can do this:

import org.mockito.Mockito;

// ...

// Create a mock object of the class that you want to test
MyClass mockMyClass = Mockito.mock(MyClass.class);

// Call the method that you want to test
mockMyClass.myMethod();

// Verify that the myMethod() method was called on the mock object
Mockito.verify(mockMyClass).myMethod();

In this example, the myMethod() method of the mockMyClass object is called, and then the verify() method is used to check that the myMethod() method was indeed called on the object.

Note that you need to import the org.mockito.Mockito class to use the Mockito methods. You also need to make sure that the mock object is created before the method under test is called, so that the mock object is used instead of the real object.

If you want to verify that a method was called a specific number of times, you can use the Mockito.verify() method with an additional argument specifying the number of times the method should have been called. For example:

Mockito.verify(mockMyClass, Mockito.times(2)).myMethod();

This will verify that the myMethod() method was called exactly 2 times on the mockMyClass object.