简体   繁体   中英

use doNothing() for method with return type in mockito

public void attestResults(List<OMInvestigationResultMutableDTO.Id> resultIds, OMRequestSpeciality speciality)
{
  List<Answer.Id> attestIds = new ArrayList<Answer.Id>();
  for (OMInvestigationResultMutableDTO.Id id : resultIds)
  {
    Answer.Id answerId = answerIdFactory.createId(id.getValue(), null);
    attestIds.add(answerId);
  }

  orderManagementServiceProvider.getOrderManagementService()
    .attestResults(attestIds, 
  RequestSpeciality.valueOf(speciality.toString()));
}

My problem is how to write unit testing for attestResults().. I want to try mock orderManagementServiceProvider.getOrderManagementService().attestResults() using doNothing() in mockito.

This is the method orderManagementServiceProvider.getOrderManagementService().attestResults,

public List<Id> attestResults(List<Id> answerIds, RequestSpeciality speciality) {
    this.accessHandler.checkAccess(AccessRights.ATTEST_RESULTS);
    ArgumentValidator.argument(answerIds, "AnswerIds").notNull().notEmpty();
    ArgumentValidator.argument(speciality, "Speciality").notNull();
    ResultCreator resultCreator = new ResultCreator(this.resultToolkitAdapter, this.pathologyReportToolkitService);
    return resultCreator.attestResults(answerIds, speciality);
}

In this case I have not permission to use powerMockito.

I mocked when(orderManagementServiceProvider.getOrderManagementService()).thenReturn(omService);

The relevant line in your code under test (cut) is this:

 orderManagementServiceProvider.getOrderManagementService() .attestResults(attestIds, RequestSpeciality.valueOf(speciality.toString())); 

Because of the violation of the Law of Demeter (Don't talk to strangers!) you have to mock both: the instance of the OrderManagementService and of the OrderManagementServiceProvider . Then you have to configure the mock of the later to return the mock of the first when getOrderManagementService() is called.

However, doNothing() only applies to void methods on spy s (a wrapped concrete instance of the dependency). void methods on mock s are not called anyway.

If your method has a return value you have to use doReturn() (or doThrow() ) so that the cut can act on the methods outcome. By default Mockito will return null , 0 or false respectively.


Warning:

The form when(dependency.someMethodWithReturnValue()).thenReturn() does call the mocked method (and throws away its result). This may lead to NPEs if the method accesses member variables or objects returned by other not configured methods in the mock.

PowerMockito is used only if you want to mock static classes. By looking at this,

orderManagementServiceProvider.getOrderManagementService()
.attestResults(attestIds, 

RequestSpeciality.valueOf(speciality.toString()));

it doesn't look like static class.

orderManagementServiceProvider = mock(OrderManagementServiceProvider.class);
doNothing().when(orderManagementServiceProvider).getOrderManagementService.attestResults(any(),any());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM