lqxue

          常用鏈接

          統計

          book

          tools

          最新評論

          如何在JUnit測試過程中,用Mock替換springContext中的Bean


                
                  ServiceClientFacade mock 
          = createMock(ServiceClientImpl.class);
                  
                  ApplicationContext ac 
          = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
                  AbstractRefreshableApplicationContext acc 
          =(AbstractRefreshableApplicationContext)ac;
                  DefaultListableBeanFactory bf 
          = (DefaultListableBeanFactory)acc.getBeanFactory();//get the beanFactory
                  bf.destroySingletons();//清除已經實例了的singleton bean
                  RootBeanDefinition rbd 
          = new RootBeanDefinition(mock.getClass());
                  bf.registerBeanDefinition(
          "serviceClient", rbd); //注冊mock bean
                  ServiceClientFacade m 
          = (ServiceClientFacade)ac.getBean("serviceClient");//get mock bean
                  //下邊是一些測試代碼,供參考
                  OpportunityFacade oppFacade 
          = (OpportunityFacade)ac.getBean("oppFacade");
                  oppFacade.saveQuote(
          null,null);
                  System.out.println(m.closeNspProcess(
          ""));




          posted on 2007-05-22 13:44 lqx 閱讀(1584) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 吉首市| 蓝山县| 溧阳市| 永嘉县| 方正县| 青川县| 上饶县| 博白县| 尼木县| 怀化市| 光泽县| 廊坊市| 漯河市| 东港市| 丰原市| 舒兰市| 城步| 平凉市| 建宁县| 达拉特旗| 阿荣旗| 邵阳市| 宕昌县| 兴城市| 泸溪县| 武功县| 山阳县| 综艺| 甘南县| 莒南县| 漯河市| 永泰县| 潮州市| 富顺县| 资源县| 牡丹江市| 建湖县| 永吉县| 长武县| 当阳市| 辽宁省|