EasyMock deep stubs

故事扮演 提交于 2020-01-24 17:14:28

问题


I have to mock the following security step using EasyMock or UnitilsMock. Could you please suggest a way to achieve this?

String id = context.getCallerPrincipal().getName();

This step is related to security. So I will not be able to create a Principle object and make a two tier mocking. I know that mockito handles such stuff easily as follows,

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
SessionContext mockContext;

But, I need a similar solution using EasyMock or Unitils Mock. The complete code, I wish to unit test is given below,

@Override
@PermitAll
public List<Employee> findAll() {
   boolean isAdmin = context.isCallerInRole(Roles.ADMIN);
   if (isAdmin) {
      return super.findAll();
   } else {
      String id = context.getCallerPrincipal().getName();
      Query query = getEntityManager().createNamedQuery("findEmployeeById");
      query.setParameter("employeeId", id);
      return query.getResultList();
   }
}

-Thanks


回答1:


If you can mock the Principal, then you can stub context.getCallerPrincipal() to return this mock, then stub mockedPrincipal.getName() to return whatever you need it to.



来源:https://stackoverflow.com/questions/9541619/easymock-deep-stubs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!