Я пытаюсь сделать тест для своего хранилища, но я получаю NullPointerException
.
Интерфейс:
public interface PersonneRepository {
void setSessionFactory(SessionFactory sessionFactory);
Personne findByLoginInformations(String email, String password);
}
Реализация интерфейса:
@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();
}
}
Класс тестирования:
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);
}
}
Вот сообщение об ошибке:
java.lang.NullPointerException в com.eheio.hello.repositories.impl.PersonneRepositoryImpl.findByLoginInformations (PersonneRepositoryImpl.java:77) в com.eheio.hello.repositories.PersonneRepositoryTest.testFj5) Personal ()