Randy Randy - 6 days ago 5
Java Question

Mockito java - test method call with different arguments

I'm trying to test this method, to see if searchProfile is called without arguments:

public void searchProfile(Long searchTerm) {
this.searchTerm = searchTerm;
searchProfile();
}

public void searchProfile() {
//...
}


This is my test case, I call the method with one argument and expect the one without the argument to be called

@Test
public void testSearchProfile() throws Exception {
CustomerProfileController sutStub = Mockito.mock(CustomerProfileController.class);

doNothing().when(sutStub).searchProfile();

sutStub.searchProfile(0L);

verify(sutStub, times(1)).searchProfile();
}


How can I make this work? Right now it just gives me an error:


Comparison Failure:

Expected: customerProfileController.searchProfile();

Actual: customerProfileController.searchProfile(0);

Answer

You should use

Mockito.when(sutStub.searchProfile(Mockito.anyLong())).thenCallRealMethod();

When preparing the mock.

Comments