关于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("[email protected]","hahhah","ddddd");
12 
13     }
14 }

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

猜你喜欢

转载自www.cnblogs.com/wang-yaz/p/9339904.html
今日推荐