我正在为实体java bean编写单元Junit测试用例。但是,当我调用EJB方法时,我得到一个空指针异常,代码如下所示
UserServiceTest.java
@PersistenceContext(unitName = "PU")
private EntityManager em;
private static UserService userService;
@Test
public void registerUser() {
userService = new UserService();
resultData = userService.registerUser(name, email, password, mobileNumber, countryId);
Assert.assertEquals(true, resultData.isSet);
Assert.assertEquals(message, resultData.message);
}
UserService.java
@PersistenceContext(unitName = "PU")
private EntityManager em;
public ResultData registerUser(String name, String email, String password, String mobileNumber, String countryId) {
try {
Query q = em.createNamedQuery("User.findByEmail", User.class).setParameter("email", email);
users = q.getResultList();
if (!users.isEmpty()) {
userDataClass.isSet = false;
userDataClass.message = "A user with this email already exists.";
return userDataClass;
}catch (Exception e) {
userDataClass.isSet = false;
}
我调试了上面的代码。当ponter来到entityManager
时Query q = em.createNamedQuery("User.findByEmail", User.class).setParameter("email", email);
我收到空指针异常。
任何人都可以帮助我吗