junit单元测试时spring的@Autowired注入为空

在测试类上添加以下注释,测试时自动创建Spring的应用上下文:

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:beans.xml" })或者@ContextConfiguration("classpath:beans.xml")

—————————————————————————————————————————————————————————

注意:@ContextConfiguration的value默认就是“locations",所以@ContextConfiguration的以上两种写法一样,如下:

@AliasFor("locations")
    String[] value() default {};

猜你喜欢

转载自blog.csdn.net/ryuhfxz/article/details/81661718