偷个懒,以上篇文章为测试
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);
}
}
运行结果: