Spring(十四)Spring整合junit

来源:互联网 发布:苹果乐器软件 编辑:程序博客网 时间:2024/06/05 18:55

偷个懒,以上篇文章为测试
[http://blog.csdn.net/su20145104009/article/details/54708025]
1.spring整合junit需要导入spring-test的jar包
2.让Junit通知spring加载配置文件
3.让spring容器自动进行注入

@RunWith(SpringJUnit4ClassRunner.class)//加载配置文件需要使用的工具@ContextConfiguration("classpath:com/scx/transferaccounts/applicationContext.xml")//spring文件路径public class Test {    @Autowired//自动注入    private AccountService service;    @org.junit.Test    public void test(){//      String xmlPath="com/scx/transferaccounts/applicationContext.xml";//      ApplicationContext aContext=new ClassPathXmlApplicationContext(xmlPath);//      AccountService service=(AccountService) aContext.getBean("accountServiceId");        service.transfer("jack", "rose", 1000);    }}

运行结果:
这里写图片描述

1 0