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 閱讀(1590) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 潢川县| 元谋县| 五原县| 海阳市| 高唐县| 高邑县| 韶关市| 天镇县| 海安县| 青阳县| 潜山县| 义马市| 莱西市| 舞钢市| 宝应县| 五台县| 呼伦贝尔市| 江陵县| 即墨市| 彩票| 巴塘县| 巴东县| 乐陵市| 淮安市| 山阴县| 株洲市| 林西县| 商丘市| 工布江达县| 遂平县| 德钦县| 容城县| 周口市| 蓬溪县| 萨嘎县| 紫云| 高尔夫| 南涧| 库尔勒市| 兴山县| 商河县|