lundi 20 novembre 2017

Reflection not working for Spring autowired parameters

I'm trying to generate an integration test without using @DataJpaTest in order to understand better the concepts. I've noticed that with Reflection I'm not able to get or set The Dao within my Service layer under test. So when it comes to access the Dao got with Reflection API it returns a NullPointerException. I've tested both Java Reflection API and ReflectionTestUtils of Spring Framework. Here is the snippet of code

UserService userService;

@Before
public void setUp(){
   userService = new UserServiceImpl();
   UserDao userDao = (UserDao) ReflectionTestUtils.getField(userService, "userDao");
   userDao.deleteAll(); //HERE RETURNS A NULLPOINTER
   ...
}

Consider that in UserServiceImpl.java I inject UserDao (Interface which extends JpaRepository using @Autowired annotation of Spring framework.

How can I access the Dao (implemented by Spring framework) from my Service? Thanks!

Aucun commentaire:

Enregistrer un commentaire