Scenario:
In service class you want to mock dao call.In service class make dao class package private (no modifier - optional).
Write test in same package (under test subfolder, not main).
Dao class has method called testingMockito(String someString);
Code example:
SomeService service; //injected
SomeDao dao = Mockito.mock(SomeDao.class);
service.setDao(dao); // or service.dao = dao;
//throw Exception when calling dao class
Mockito.doThrow(someException).when(dao).testingMockito(Mockito.anyString());
Verify
Mockito.verify(simCardServiceMock, Mockito.times(1)).findSimCard(simIdentifier);Mock void methods:
1st service returns some response, 2nd service calls void method.
CustomerSearchService customerSearchService = Mockito.mock(CustomerSearchService.class);
Mockito.when(customerSearchService.searchSubscription(providerId, searchCriteria)).thenReturn(subscription);
final SubscriptionService subscriptionService = Mockito.mock(SubscriptionService.class);
Mockito.doNothing()
.when(subscriptionService)
.activateSubscription(providerId, subscription.getId());
Ref:
https://stackoverflow.com/questions/14889951/how-to-verify-a-method-is-called-two-times-with-mockito-verify
http://www.baeldung.com/mockito-behavior
https://stackoverflow.com/questions/2276271/how-to-make-mock-to-void-methods-with-mockito
No comments:
Post a Comment