SSM中进行Junit单元测试时无法注入service
生活随笔
收集整理的這篇文章主要介紹了
SSM中进行Junit单元测试时无法注入service
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
在SSM項目中進行Junit單元測試時調用外部的service時,在使用時打斷點發現為空。
代碼如下:
public class AlipayTester {private PassOrderService passOrderService;@Autowiredpublic void setPassOrderService(PassOrderService passOrderService) {this.passOrderService = passOrderService;}@Testpublic void alipay() {try {PassOrder passOrder = passOrderService.getByPrimaryKey("89387");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}} }然后會提示passOrderService為空。
解決
添加注解:
@RunWith(SpringJUnit4ClassRunner.class) //告訴junit spring配置文件 @ContextConfiguration("classpath:spring.xml")?
添加后代碼:
@RunWith(SpringJUnit4ClassRunner.class) //告訴junit spring配置文件 @ContextConfiguration("classpath:spring.xml") public class AlipayTester {private PassOrderService passOrderService;@Autowiredpublic void setPassOrderService(PassOrderService passOrderService) {this.passOrderService = passOrderService;}@Testpublic void alipay() {try {PassOrder passOrder = passOrderService.getByPrimaryKey("89387");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}} }?
總結
以上是生活随笔為你收集整理的SSM中进行Junit单元测试时无法注入service的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Okhttp3中设置超时的方法
- 下一篇: Linux-Ubuntu Server