私はNullPointerException
に直面しています。 私はJUnitテストを実行しようとしていて、何とかNullPointerException
を受け取ります。JUnitテスト; nullpointer例外
私が書いたテストは以下の通りです。一方
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)
デバッガの使用方法を学びます。 –