關於Test類中不能使用Autowired注入bean的問題


在測試類中使用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");

    }
}

 


免責聲明!

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



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