在測試類中使用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 }
大家寫代碼一定要仔細啊!不然多的時間浪費了。