文章詳情頁
SpringBoot Test類注入失敗的解決
瀏覽:3日期:2023-03-23 10:50:49
如下所示
本來 bookService的引用一直是null。
導致每次測試都報空指針異常。
然后現在繼承相應的 ApplicationTests類,然后使用@Component將該類注冊為組件。就可以正常注入了。
補充:關于springboot test @Mapper ,@Autiwired注入無效的問題
@SpringBootTest()@RunWith(SpringRunner.class)public class ProductMapperTest { @Autowired ProductMapper productMapper;
為了給mapper接口 自動根據一個添加@Mapper注解的接口生成一個實現類
怎么注入都是失敗,ProductMapper 使用@Mapper 注解,這個不能注入到spring 容器中(其中原因還是不了解)。
@Autowired 注入不進去的。
@Repository@Mapperpublic interface ProductMapper {
這下能注入容器中了。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。
標簽:
Spring
相關文章:
排行榜
