【问题标题】:EasyMock to test SecurityExceptionEasyMock 测试 SecurityException
【发布时间】:2011-11-26 23:00:08
【问题描述】:

我正在尝试使用 easyMock 编写一个测试,在以下代码中测试 SecurityException。

例如。对于 NumberFormatException 我使用下面的。

EasyMock.expect(mockEntityManager.find(UserProfile.class,"abc")).andThrow(new NumberFormatException());

关于抛出 SecurityException 的任何想法?

public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {

EntityManager entityManager = (EntityManager)Component.getInstance("entityManager");

  if (s == null || s.equals("null")) {
        return null;      } else {
        try {
            long i = Long.parseLong(s);
            return entityManager.find(UserProfile.class, i);
        } catch (NumberFormatException e) {
            logger.error(e);
        } catch (SecurityException e) {
            logger.error(e);
        }         }

    return null;  }

【问题讨论】:

  • 不确定我是否理解。如果您只是将 new NumberFormatException() 替换为 new SecurityException(),什么不起作用?

标签: java unit-testing easymock securityexception


【解决方案1】:

我感觉您还没有编写该代码,这就是为什么您想知道可能会抛出SecurityException 的原因。答案是什么都没有,只要您使用的是良好的 EntityManager 实现。

EntityManager.find()enter link description here 的文档版本不会抛出 SecurityException但是如果您在使用自定义版本的 EntityManager 的 J2EE 应用服务器中运行该代码,则可能会引发该异常......但我认为不应该。

【讨论】:

    【解决方案2】:

    感谢您的回复。这是我预期 SecurityException 所做的。

    MyClass abc = new MyClass();
    
    EasyMock.expect(mockEntityManager.find(MyClass.class,111L)).andThrow(new SecurityException());
    
    EasyMock.replay(mockEntityManager);
    
    Object target = abc.getAsObject(mockFacesContext, mockUiComponent,"111");
    
    Assert.assertEquals(null, target);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多