我正在尝试使用Mockito创建测试,但是我收到了NullPointerException。
以下是会话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;
}
}
这是测试类:
package edu.city.set.eia.citypress.mocking.beans;
import edu.city.set.eia.citypress.beans.RegisUserFacade;
import edu.city.set.eia.citypress.model.RegisUser;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class RegisUserTest {
private final transient EntityManager mockedEm =
mock(EntityManager.class);
private final transient Query mockedQuery =
mock(Query.class);
private static final String EMAIL = "test_email";
private static final String INVALID_EMAIL = "test_email_invalid";
@Before
public final void setUp() {
when(mockedEm.createQuery("Select r from RegisUser r where "
+ "r.email = :email")).thenReturn(mockedQuery);
when(mockedQuery.setParameter("email", EMAIL)).thenReturn(mockedQuery);
when(mockedQuery.setParameter("email", INVALID_EMAIL)).thenReturn(mockedQuery);
}
@Test
public void testValidEmail() throws Exception {
final RegisUser expectedUser = new RegisUser();
expectedUser.setEmail(EMAIL);
when(mockedQuery.setParameter("email", EMAIL)).thenReturn(mockedQuery);
when(mockedQuery.getSingleResult()).thenReturn(expectedUser);
RegisUserFacade bean = new RegisUserFacade();
bean.setEntityManager(mockedEm);
RegisUser user = bean.findByEmail(EMAIL);
assertNotNull(user);
assertEquals(EMAIL, user.getEmail());
}
@Test(expected = Exception.class)
public void testInValidEmail() throws Exception {
final RegisUser expectedUser = new RegisUser();
expectedUser.setEmail(EMAIL);
when(mockedQuery.setParameter("email", INVALID_EMAIL)).thenReturn(mockedQuery);
when(mockedQuery.getSingleResult()).thenThrow(new Exception());
RegisUserFacade bean = new RegisUserFacade();
bean.setEntityManager(mockedEm);
bean.findByEmail(INVALID_EMAIL);
}
}
我得到的错误是testValidEmail caused an ERROR: java.lang.NullPointerException
。
答案 0 :(得分:4)
我对Mockito并不熟悉,但看起来你的字符串不匹配。如果Mockito区分大小写,则可以解释为什么query
对象为null
。
when(mockedEm.createQuery("select r from RegisUser r where r.email = :email")).thenReturn(mockedQuery);
请注意“select”中的小写“s”,它与您在bean中的内容匹配。
答案 1 :(得分:1)
我遇到了同样的问题,我认为问题更为根本。 mockedQuery.setParameter方法应该修改mockedQuery而不是简单地返回一些东西。我使用thenAnswer方法而不是thenReturn方法解决了这个问题。
Mockito.when(mockedQuery.setParameter(anyString(), anyString())).then(
new Answer<Query>() {
public Query answer(InvocationOnMock invocation) {
Object[] argumentsToTest = invocation.getArguments();
if (!"email".equals((String) argumentsToTest[0])) {
return findUserByIdQuery;
}
if (EMAIL.equals((String) argumentsToTest[1])) {
Mockito.when(mockedQuery.getSingleResult()).thenReturn(expectedUser);
} else {
Mockito.when(mockedQuery.getSingleResult()).thenThrow(new Exception());
}
return mockedQuery;
}
}
);
通过这种方式,您可以将整个模拟放入@Before块并将测试留给测试行为。希望这有帮助!
答案 2 :(得分:-1)
如果在NullPointerException
处抛出query.setParameter("email", email);
,则query
为null
。也许您需要在该查询字符串中终止;
。