尝试用单元测试做spring注入调用service
public class GoodsCategoryServiceImplTest extends TestCase{
public ApplicationContext context=null;
protected void setUp() throws Exception {
String[] files={"appContext-core.xml","appContext-memcached.xml","appContext-mvc.xml"};
context=(ApplicationContext)new ClassPathXmlApplicationContext(files);
}
public void test(){
GoodsCategoryService goodsCategoryService=(GoodsCategoryService) context.getBean("goodsCategoryService");
System.out.println(goodsCategoryService.getAll(new HashMap()).size());
}
}
本文介绍了一个使用Spring框架进行依赖注入的单元测试示例。通过ClassPathXmlApplicationContext加载配置文件appContext-core.xml、appContext-memcached.xml和appContext-mvc.xml来初始化ApplicationContext。测试中获取GoodsCategoryService实例并调用getAll方法,输出返回结果的大小。

被折叠的 条评论
为什么被折叠?



