I am write UT with Mockito, and I want replace my mock function(which DB select operation)
class DataBaseSelect {
List<Long> selectDataFromDB(Long key){
List<Long> result = db.select(key);
return result;
}
}
with new function (mock db select using map) I wrote in Test class;
class MapSelect {
List<Long> selectDataFromMap(Long key){
List<Long> result = map.get(key);
return result;
}
}
and I want return the right value with right key input
I try to do this using ArgumentCaptor as below, but it did not work as I want
ArgumentCaptor<Long> argumentCaptor = ArgumentCaptor.forClass(Long.class);
Mockito.when(dataBaseSelect.selectDataFromDB(argumentCaptor.capture())).thenReturn(MapSelect.selectDataFromMap(argumentCaptor.getValue()));
//some thing else here ,
I want when call dataBaseSelect.selectDataFromDB, it will be mock and then return result from MapSelect.selectDataFromMap and argument is passed from dataBaseSelect.selectDataFromDB
We can mock runInGround(String location) method inside the PersonTest class as shown below. Instead of using mock(class) here we need to use Mockito. spy() to mock the same class we are testing. Then we can mock the method we want as follows.
Mocks are used to create fully mock or dummy objects. It is mainly used in large test suites. Spies are used for creating partial or half mock objects. Like mock, spies are also used in large test suites.
Mockito allows us to partially mock an object. This means that we can create a mock object and still be able to call a real method on it. To call a real method on a mocked object we use Mockito's thenCallRealMethod().
If you need substitute your own implementation of method, you can do something like that:
// create method substitution
Answer<List<Long>> answer = invocation -> mapSelect.selectDataFromMap((Long) invocation.getArguments()[0]);
// Mock method
Mockito.doAnswer(answer).when(dataBaseSelect).selectDataFromDB(anyLong());
Your solution is incorrect, cause captors are intended for method call verification (how many times method was called), example:
// method calling: mockedObject.someMethod(new Person("John"));
//...
ArgumentCaptor<Person> capture = ArgumentCaptor.forClass(Person.class);
verify(mockedObject).someMethod(capture.capture());
Person expected = new Person("John");
assertEquals("John", capture.getValue().getName());
in this example expected that 'someMethod' was called one time and return person "John"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With