EasyMock 测试 SecurityException
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我感觉您还没有编写该代码,这就是为什么您想知道什么可能会抛出
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.感谢您的回复..这就是我期望 SecurityException 所做的事情。
Thanks for your responses..here is what I did to expect SecurityException.