简体   繁体   English

使用JUnit进行Spring MVC测试

[英]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 建议以下

  • @RunWith(MockitoJUnitRunner.class) to the Test Class @RunWith(MockitoJUnitRunner.class)到测试类
  • Mock session = sessionFactory.getCurrentSession(); 模拟会话= sessionFactory.getCurrentSession();
  • Mock .setParameter("password", password); 模拟.setParameter(“ password”,password); for Query 查询
  • Then in the same statement, mock for .setParameter("email", email) using then 然后在同一条语句中,使用then来模拟.setParameter(“ email”,email)
  • Mock for .createQuery("FROM Personne p WHERE p.email = :email AND p.password = :password") .createQuery(“ FROM Personne p.email =:email AND p.password =:password”的模拟文件)
  • Mock for query.uniqueResult() 模拟query.uniqueResult()
  • For personneRepositoryImpl.findByLoginInformations("ayoub", "aaaa") should NOT be mocked. 对于personneRepositoryImpl.findByLoginInformations(“ ayoub”,“ aaaa”)不应进行模拟。 Must be called. 必须调用。
  • Finally assert. 最后断言。

Hope, it might help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM