Search code examples
mockingmockitoeasymockunitils

EasyMock deep stubs


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


Solution

  • 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.