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中设置超时的方法
- 下一篇: html中设置td中内容的垂直位置