關於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 }

 

大家寫代碼一定要仔細啊!不然多的時間浪費了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM