开发者

EasyMock to test SecurityException

开发者 https://www.devze.com 2023-04-08 20:26 出处:网络
I am trying to use easyMock to write a test, that tests Securit开发者_JAVA技巧yException in the following code.

I am trying to use easyMock to write a test, that tests Securit开发者_JAVA技巧yException 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;  }


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消