Jeetendra Ahuja
Jeetendra Ahuja

Reputation: 177

Skipping sending one of the method parameters in Junit

I am calling one public method in my JUnit and skipping call to private method which is being called by public one.

SampleService sampleServiceSpy = Mockito.spy(sampleService); // I have reference to it through @InjectMocks

String str = Mockito.doReturn("Hiii").when(sampleServiceSpy ).sendRequestToAnotherComponent(<ARG1>,"?",<ARG3>);

String res = sampleServiceSpy.processRequest(<ARG1>, <ARG2>);

Here processRequest() is public method and private method is sendRequestToAnotherComponent() which I am skipping but problem is this method expects second argument as directory path which is random UUID generated differently every-time so I can't mock it. (Shown as ? in sample code)

Is there any way I can pass any value and can skip this method?

I checked Mockito.anyString() is used to create mock objects and hence can't use it for this test case.

Upvotes: 0

Views: 332

Answers (1)

Andrew S
Andrew S

Reputation: 2756

To answer your direct question - use matchers - for example:

 Mockito.doReturn("Hiii").when(sampleServiceSpy).sendRequestToAnotherComponent(eq(<ARG1>),any(UUID.class), eq(<ARG3>));

But instead of attempting to mock the method of the class that is being tested, inject a mock of the other component. That way, the test can verify() the other component was invoked.

Upvotes: 1

Related Questions