EasyMock 测试 SecurityException

发布于 2024-12-06 20:31:37 字数 851 浏览 1 评论 0原文

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

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

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

关于抛出 SecurityException 的情况有什么想法吗?

公共对象 getAsObject(FacesContext facesContext, UIComponent uiComponent, 字符串) {

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

 if (s == null || s.equals("null")) {
        返回空值; } 别的 {
        尝试 {
            长 i = Long.parseLong(s);
            返回entityManager.find(UserProfile.class, i);
        } catch (NumberFormatException e) {
            记录器.error(e);
        } catch (SecurityException e) {
            记录器.error(e);
        } }

    返回空值; }

I am trying to use easyMock to write a test, that tests SecurityException in the following code.

eg. for NumberFormatException I use the below.

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

Any ideas on what to expect to throw 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;  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2024-12-13 20:31:37

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

EntityManager.find() 的记录版本在此处输入链接描述不会抛出SecurityException但是如果您在使用自定义版本的 EntityManager 的 J2EE 应用服务器内运行该代码,则它可能会抛出该异常...但我认为不应该这样做。

I have the feeling that you haven't written that code, and that's why you're wondering what might throw SecurityException. The answer is nothing, as long as you're using a good implementation of EntityManager.

The documented version of EntityManager.find()enter link description here doesn't throw SecurityException. BUT if you're running that code inside a J2EE app server that uses a custom version of EntityManager, it could be that it throws that exception... But I don't think it should.

巡山小妖精 2024-12-13 20:31:37

感谢您的回复..这就是我期望 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);

Thanks for your responses..here is what I did to expect 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文