EasyMock Deep Plugs

I have to make fun of the next security step using EasyMock or UnitilsMock. Could you suggest a way to achieve this?

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

This step is security related. Therefore, I can’t create the Principle object and do about the two-level level. I know that mockito easily handles such things as follows:

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

But I need a similar solution using EasyMock or Unitils Mock. The full code I want unit test is 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

+2
source share
1 answer

If you can mock Principal , then you can stub context.getCallerPrincipal() to return this layout, and then mockedPrincipal.getName() to return everything you need.

+1
source

Source: https://habr.com/ru/post/956165/


All Articles