[英]Spring MVC testing with JUnit
I'm trying to do some test for my repository, but I'm getting a NullPointerException
. 我正在尝试对我的存储库进行一些测试,但是却遇到了
NullPointerException
。
Interface: 接口:
public interface PersonneRepository {
void setSessionFactory(SessionFactory sessionFactory);
Personne findByLoginInformations(String email, String password);
}
Interface implementation: 接口实现:
@Repository
public class PersonneRepositoryImpl implements PersonneRepository {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Personne findByLoginInformations(String email, String password){
Session session;
try {
session = sessionFactory.getCurrentSession();
} catch (HibernateException e) {
session = sessionFactory.openSession();
}
Query query = session
.createQuery("FROM Personne p WHERE p.email = :email AND p.password = :password")
.setParameter("email", email)
.setParameter("password", password);
return (Personne)query.uniqueResult();
}
}
Test class : 测试类别:
public class PersonneRepositoryTest {
@Mock
private SessionFactory sessionFactory;
@InjectMocks
private PersonneRepositoryImpl personneRepositoryImpl;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testFindPersonne(){
Personne p = new Personne();
p.setId(1);
when(personneRepositoryImpl.findByLoginInformations("ayoub", "aaaa")).thenReturn(p);
assertEquals(p, personneRepositoryImpl.findById(1));
verify(personneRepositoryImpl).findById(1);
}
}
Here's the error message: 这是错误消息:
java.lang.NullPointerException at com.eheio.hello.repositories.impl.PersonneRepositoryImpl.findByLoginInformations(PersonneRepositoryImpl.java:77) at com.eheio.hello.repositories.PersonneRepositoryTest.testFindPersonne(PersonneRepositoryTest.java:31) com.eheio.hello.repositories.PersonneRepositoryTest.testFindPersonne(Personne31)上的com.eheio.hello.repositories.impl.PersonneRepositoryImpl.findByLoginInformations(PersonneRepositoryImpl.java:77)上的java.lang.NullPointerException
Suggesting the following 建议以下
Hope, it might help you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.