0

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;  }
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94
  • 2
    Not sure I understand. What doesn't work if you just replace new NumberFormatException() by new SecurityException()? – JB Nizet Sep 28 '11 at 14:51

2 Answers2

0

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.

Augusto
  • 28,839
  • 5
  • 58
  • 88
0

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);
Dchucks
  • 1,189
  • 5
  • 22
  • 48
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94