在測試類中使用AutoWired注解一直不能獲取到Bean,調用方法時一直報空指針異常,我有在其他類中使用AutoWired試了下,發現能夠生效。問題應該就是處在Test類中,后面找了半天終於找到問題了!!!
解決方法:
測試類上加如下注解:
@SpringBootTest
@RunWith(SpringRunner.class)
@SpringBootTest @RunWith(SpringRunner.class) public class AccountEmailServiceImplTest { @Autowired private AccountEmailService accountEmailService; @Test public void sendMail() throws Exception { accountEmailService.sendMail("xxx@163.com","hahhah","ddddd"); } }