2

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

rrufai
  • 1,475
  • 14
  • 26
Bala
  • 1,193
  • 2
  • 12
  • 34

1 Answers1

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.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Thanks for your suggestion. Should I have to change the Class Under test to `Principal principal = context.getCallerPrincipal(); String caller=principal.getName();` then mock the Pricipal in my test and inject it into the Class under test? – Bala Mar 03 '12 at 10:59
  • I don't think you need to change the SUT. – Dawood ibn Kareem Mar 04 '12 at 07:23