Seam 单元测试中的 EntityManger null

发布于 2024-12-03 09:54:38 字数 1916 浏览 1 评论 0原文

下面是我试图测试的代码,但在entityManager.find上出现空指针异常,因为entityManager = null。有什么建议吗?

@Name("UserProfileConverter")
@BypassInterceptors
@Converter(forClass= UserProfile.class)
public class UserProfileConverter implements javax.faces.convert.Converter {

    @Logger
    private static Log logger;

    public Object getAsObject(FacesContext arg0, UIComponent uiComponent, String s) {
           EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");

        UserProfile p;

        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;
    }

    public String getAsString(FacesContext context, UIComponent uiComponetn, Object arg2) {
        return ((CsaRole)arg2).getCsaRoleId() + "";
    }

}

这是我的测试课..

public class UserProfileConverterTest extends SeamTest {

    private UserProfileConverter converter;
      private FacesContext mockFacesContext;
      private UIComponent mockUiComponent;

      private final static Logger logger = Logger.getLogger(UserProfileConverterTest.class);

      @BeforeClass
      public void setup() {
          converter = new UserProfileConverter();
      }
      @Test
      public void testGetAsObject()
              throws Exception {
              new ComponentTest() {

              @Override
              protected void testComponents() throws Exception {

              String value =   "11111111111";
              converter.getAsObject(mockFacesContext, mockUiComponent, value);

              }
          }.run();
      }


}

Below is the code I am trying to test, but getting null pointer exception on entityManager.find coz entityManager = null. Any suggestions?

@Name("UserProfileConverter")
@BypassInterceptors
@Converter(forClass= UserProfile.class)
public class UserProfileConverter implements javax.faces.convert.Converter {

    @Logger
    private static Log logger;

    public Object getAsObject(FacesContext arg0, UIComponent uiComponent, String s) {
           EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");

        UserProfile p;

        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;
    }

    public String getAsString(FacesContext context, UIComponent uiComponetn, Object arg2) {
        return ((CsaRole)arg2).getCsaRoleId() + "";
    }

}

Here is my test class..

public class UserProfileConverterTest extends SeamTest {

    private UserProfileConverter converter;
      private FacesContext mockFacesContext;
      private UIComponent mockUiComponent;

      private final static Logger logger = Logger.getLogger(UserProfileConverterTest.class);

      @BeforeClass
      public void setup() {
          converter = new UserProfileConverter();
      }
      @Test
      public void testGetAsObject()
              throws Exception {
              new ComponentTest() {

              @Override
              protected void testComponents() throws Exception {

              String value =   "11111111111";
              converter.getAsObject(mockFacesContext, mockUiComponent, value);

              }
          }.run();
      }


}

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

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

发布评论

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

评论(1

独夜无伴 2024-12-10 09:54:38

公共类 UserProfileConverterTest 扩展 SeamT​​est {

EntityManager mockEntityManager;
私有 UserProfileConverter 转换器;
UIComponent模拟UiComponent = null;
MockFacesContextmockFacesContext = null;

@课前
公共无效设置(){
    转换器 = 新 CsaUserProfileConverter();
    mockEntityManager = EasyMock.createMock(EntityManager.class);
}


类 BaseComponentTest 扩展 ComponentTest {

    protected void testComponents() 抛出异常 {

    ScopeType.EVENT.getContext().set("entityManager",mockEntityManager);
    }
}

@测试
公共无效testGetAsObject()抛出异常{
    新的 BaseComponentTest() {
        protected void testComponents() 抛出异常 {
            super.testComponents();

        UserProfile 预期结果 = new UserProfile();
        EasyMock.expect(mockEntityManager.find(UserProfile.class,1L)).andReturn(expectedResult);


        //重播模拟 
        EasyMock.replay(mockEntityManager);

        对象目标 = converter.getAsObject(mockFacesContext,mockUiComponent,"1");
        Assert.assertEquals(expectedResult, 目标);

        //验证模拟 
        EasyMock.verify(mockEntityManager);

        }
    }。跑步();
}

public class UserProfileConverterTest extends SeamTest {

EntityManager mockEntityManager;
private UserProfileConverter converter;
UIComponent mockUiComponent = null;
MockFacesContext mockFacesContext = null;

@BeforeClass
public void setup() {
    converter = new CsaUserProfileConverter();
    mockEntityManager = EasyMock.createMock(EntityManager.class);
}


class BaseComponentTest extends ComponentTest {

    protected void testComponents() throws Exception {

    ScopeType.EVENT.getContext().set("entityManager", mockEntityManager);
    }
}

@Test
public void testGetAsObject() throws Exception {
    new BaseComponentTest() {
        protected void testComponents() throws Exception {
            super.testComponents();

        UserProfile expectedResult = new UserProfile();
        EasyMock.expect(mockEntityManager.find(UserProfile.class,1L)).andReturn(expectedResult);


        //Replay Mock 
        EasyMock.replay(mockEntityManager);

        Object target = converter.getAsObject(mockFacesContext,mockUiComponent,"1");
        Assert.assertEquals(expectedResult, target);

        //Verify the Mock 
        EasyMock.verify(mockEntityManager);

        }
    }.run();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文