今天在配置JUint做Service层测试,发现Bean一直无法注入,刷了很多的博客,一直没有成功。
先附上最后成功的代码
/**
* @author Jame
* @date 2018/07/24 16:47
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BackendApplication.class)
@ContextConfiguration(locations = { "classpath*:*-config.xml"})
public class MessageNoticeServiceImplTest {
@Resource
private MessageNoticeService messageNoticeService;
@Test
public void sendMessage() {
Assert.assertNull(messageNoticeService.sendMessage("标题","内容",10001,null));
}
}
关键点在于:
1、@SpringBootTest(classes = BackendApplication.class)
作为SpringBoot测试项目启动,标注启动的Application。亲测缺失注解和不指定Application都无法注入成功。
2、@ContextConfiguration(locations = { "classpath*:*-config.xml"})
或者是 @ContextConfiguration(locations = { "c