关于Test类中不能使用Autowired注入bean的问题

在测试类中使用AutoWired注解一直不能获取到Bean,调用方法时一直报空指针异常,我有在其他类中使用AutoWired试了下,发现能够生效。问题应该就是处在Test类中,后面找了半天终于找到问题了!!!

解决方法:

 测试类上加如下注解:

@SpringBootTest
@RunWith(SpringRunner.class)
 1 @SpringBootTest
 2 @RunWith(SpringRunner.class)
 3 public class AccountEmailServiceImplTest {
 4 
 5     @Autowired
 6     private AccountEmailService accountEmailService;
 7 
 8     @Test
 9     public void sendMail() throws Exception {
10 
11         accountEmailService.sendMail("xxx@163.com","hahhah","ddddd");
12 
13     }
14 }

 

大家写代码一定要仔细啊!不然多的时间浪费了。

posted @ 2018-07-20 09:59  低调的小白  阅读(11044)  评论(0编辑  收藏  举报