EasyMock深存根 [英] EasyMock deep stubs

查看:107
本文介绍了EasyMock深存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用EasyMock或UnitilsMock模拟以下安全步骤。

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();

此步骤与安全性有关。因此,我将无法创建Principled对象并进行两层模拟。我知道Mockito可以轻松地按如下方式处理此类内容,

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;

但是,我需要使用EasyMock或Unitils Mock的类似解决方案。完整的代码,我希望进行单元测试,如下所示。

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();
   }
}

-谢谢

推荐答案

如果您可以模拟 Principal ,则可以对 context.getCallerPrincipal( )返回此模拟,然后存根 mockedPrincipal.getName()返回您需要的内容。

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.

这篇关于EasyMock深存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆