Spring not autowiring in unit tests with JUnit

后端 未结 5 733
盖世英雄少女心
盖世英雄少女心 2020-12-03 02:33

I test the following DAO with JUnit:

@Repository
public class MyDao {

    @Autowired
    private SessionFactory sessionFactory;

    // Other stuff here

}
         


        
5条回答
  •  长情又很酷
    2020-12-03 03:08

    Missing Context file location in configuration can cause this, one approach to solve this:

    • Specifying Context file location in ContextConfiguration

    like:

    @ContextConfiguration(locations = { "classpath:META-INF/your-spring-context.xml" })
    

    More details

    @RunWith( SpringJUnit4ClassRunner.class )
    @ContextConfiguration(locations = { "classpath:META-INF/your-spring-context.xml" })
    public class UserServiceTest extends AbstractJUnit4SpringContextTests {}
    

    Reference:Thanks to @Xstian

提交回复
热议问题