JUnit测试;空指针异常

时间:2011-12-16 06:11:35

标签: java junit nullpointerexception

我正面临NullPointerException。 我正在尝试执行JUnit测试并以某种方式接收NullPointerException

我写的测试如下;

package edu.city.set.eia.citypress.unittest.beans;

import edu.city.set.eia.citypress.model.RegisUser;
import edu.city.set.eia.citypress.beans.RegisUserFacade;
import org.junit.Test;
import static org.junit.Assert.*;

public class RegisUserUTest {

  private static final String EMAIL = "test_email";
  private static final String INVALID_EMAIL = "test_email_invalid";

  @Test
  public void testValidEmail() throws Exception {
    TestEmail bean = new TestEmail();
    RegisUser user = bean.findByEmail(EMAIL);
    assertNotNull(user);
    assertEquals(EMAIL, user.getEmail());
  }

  @Test(expected = Exception.class)
  public void testInValidUser() throws Exception {
    TestEmail bean = new TestEmail();
    bean.findByEmail(INVALID_EMAIL);
  }

  private static class TestEmail extends RegisUserFacade {

    private RegisUser user;

    public TestEmail() {
      user = new RegisUser();
      user.setEmail(EMAIL);
    }

    @Override
    public RegisUser findUser(String email) {
      if (email.equals(user.getEmail())) {
        return user;
      }
      return null;
    }
  }
}

另一方面, 会话bean如下;

package edu.city.set.eia.citypress.beans;

import edu.city.set.eia.citypress.model.RegisUser;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Stateless
public class RegisUserFacade extends AbstractFacade<RegisUser> implements RegisUserFacadeLocal {
    @PersistenceContext(unitName = "citypress_PU")
    private EntityManager em;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public void setEntityManager(EntityManager em) {
        this.em = em;
    }


    public RegisUserFacade() {
        super(RegisUser.class);
    }


    @Override
    public RegisUser findByUsername(String username) {
        Query query = this.getEntityManager().createQuery("select r from RegisUser r where username=:username");
        query.setParameter("username", username);
        RegisUser regisuser;
        try { 
            regisuser = (RegisUser) query.getSingleResult();
        }
        catch (Exception e){
            regisuser = null;
        }
        return regisuser;
    }

    @Override
    public RegisUser findByUserPass(String username, String password){
        Query query = this.getEntityManager().createNamedQuery("RegisUser.findByUserPass");
        query.setParameter("username", username);
        query.setParameter("password", password);
        RegisUser regisuser;

        try { // To prevent javax.persistence.NoResultException
            regisuser = (RegisUser) query.getSingleResult();
        }
        catch (Exception e){
            regisuser = null;
        }

        return regisuser;
    }

    @Override
    public RegisUser findByEmail(String email) {
        Query query = em.createQuery("select r from RegisUser r where r.email = :email");
        query.setParameter("email", email);
        RegisUser regisuser;
        try { 
            regisuser = (RegisUser) query.getSingleResult();
        }
        catch (Exception e){
            regisuser = null;
        }
        return regisuser;
    }
}

当我运行测试时,它会显示NullPointerException

java.lang.NullPointerException
    at edu.city.set.eia.citypress.beans.RegisUserFacade.findByEmail(RegisUserFacade.java:63)
    at edu.city.set.eia.citypress.unittest.beans.RegisUserUTest.testValidEmail(RegisUserUTest.java:16)

2 个答案:

答案 0 :(得分:0)

em似乎在

中为空
Query query = em.createQuery("select r from RegisUser r where r.email = :email");

如果这一行是63(可以在Stacktrace中看到行号: RegisUserFacade.java 63

答案 1 :(得分:0)

以下代码似乎失败了。 EntityManager为null,我看不到它由Junit设置。如果你想从容器中测试它或者使用嵌入式玻璃鱼,我建议你使用Ejb3Unit

Query query = this.getEntityManager().createQuery("select r from RegisUser r where username=:username");