云自無心水自閑

          天平山上白云泉,云自無心水自閑。何必奔沖山下去,更添波浪向人間!
          posts - 288, comments - 524, trackbacks - 0, articles - 6
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2007年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          隨筆檔案

          積分與排名

          • 積分 - 1129171
          • 排名 - 27

          最新評論

          appfuse 1.9.4 學習心得 -創建新的Manager

          Posted on 2007-03-14 15:09 云自無心水自閑 閱讀(783) 評論(0)  編輯  收藏 所屬分類: JavaAppfuse

          這幾天在作Raible的Tutorial,前兩天做了Create Dao & POJO, 今天做Create Manager。
          中間有一些過程與大家分享一下:
          在PersonManagerTest的單元測試中,在Raible的教程中,說需要在applicationContext-service.xml中添加PersonManager這個bean的配置,但是實際上我沒有添加配置,單元測試就成功了,為什么?
          原因:在教程中的Test使用了Mock,好像并沒有真正的使用其他關聯的類,現在采用真正的測試類,下面注釋掉的是教程中的測試類。:

          package ?org.appfuse.service;

          // import?java.util.List;

          import ?org.appfuse.model.Person;
          import ?org.springframework.context.support.ClassPathXmlApplicationContext;
          import ?org.springframework.dao.DataAccessException;
          import ?org.springframework.context.ApplicationContext;

          public ? class ?PersonManagerTest? extends ?BaseManagerTestCase? {

          ????
          private ?Person?person? = ? null ;
          ????
          private ?PersonManager?mgr? = ? null ;

          ????
          protected ? void ?setUp()? throws ?Exception? {
          ????????
          super .setUp();
          ????????ApplicationContext?ctx?
          = ? new ?ClassPathXmlApplicationContext( " org/appfuse/service/applicationContext-*.xml " );
          ????????mgr?
          = ?(PersonManager)?ctx.getBean( " personManager " );
          ????}


          ????
          protected ? void ?tearDown()? throws ?Exception? {
          ????????
          super .tearDown();
          ????????mgr?
          = ? null ;
          ????}

          ????
          ????
          public ? void ?testGetPerson()? throws ?Exception? {
          ????????person?
          = ?mgr.getPerson( " 1 " );

          ????????assertNotNull(person.getFirstName());
          ????}


          ????
          public ? void ?testSavePerson()? throws ?Exception? {
          ????????person?
          = ?mgr.getPerson( " 1 " );
          ????????person.setFirstName(
          " test " );

          ????????mgr.savePerson(person);
          ????????assertEquals(person.getFirstName(),?
          " test " );
          ????}


          ????
          public ? void ?testAddAndRemovePerson()? throws ?Exception? {
          ????????person?
          = ? new ?Person();
          ????????person?
          = ?(Person)?populate(person);

          ????????mgr.savePerson(person);
          ????????assertEquals(person.getFirstName(),?
          " Bill " );
          ????????assertNotNull(person.getId());

          ????????log.debug(
          " removing?person,?personId:? " ? + ?person.getId());

          ????????mgr.removePerson(person.getId().toString());

          ????????
          try ? {
          ????????????person?
          = ?mgr.getPerson(person.getId().toString());
          ????????????fail(
          " Person?found?in?database " );
          ????????}
          ? catch ?(DataAccessException?dae)? {
          ????????????log.debug(
          " Expected?exception:? " ? + ?dae.getMessage());
          ????????????assertNotNull(dae);
          ????????}

          ????}

          }



          現在如果不在applicationContext-service.xml中添加bean的配置后,JUnit報錯說找不到personManager。另外還需要把applicationContext-hibernate.xml和applicationContext-resources.xml也拷貝到WEB-INF/src/org/appfuse/service目錄下,這樣Context才能Load成功。
          現在運行Test,GetPerson和SavePerson都測試通過,而AddAndRemovePerson報了一個NullPointerExceptoin錯誤。原來缺少了PersonManagerTest.properties這個文件,這個文件是用于populate初始化Person實例的。



          主站蜘蛛池模板: 华坪县| 衡山县| 双桥区| 宁城县| 博野县| 十堰市| 财经| 镇平县| 伊通| 丰原市| 兴化市| 黔江区| 屯留县| 南开区| 孝感市| 西盟| 永昌县| 藁城市| 高雄市| 银川市| 宜兰县| 西藏| 工布江达县| 当涂县| 金溪县| 盘山县| 丹凤县| 岳池县| 翁源县| 西华县| 鞍山市| 道孚县| 沁水县| 永顺县| 榆林市| 五寨县| 东乌珠穆沁旗| 会泽县| 宜兰市| 洮南市| 白城市|