云自無心水自閑

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

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

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

          這幾天在作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實例的。



          主站蜘蛛池模板: 平湖市| 彰化县| 五台县| 祁东县| 射阳县| 张家港市| 井陉县| 永善县| 高邮市| 永丰县| 荣成市| 镇康县| 博白县| 文水县| 焦作市| 通江县| 和硕县| 鄂尔多斯市| 武鸣县| 刚察县| 吉木萨尔县| 夹江县| 岳阳市| 靖宇县| 武清区| 射洪县| 杨浦区| 皋兰县| 江山市| 静海县| 建宁县| 丘北县| 偏关县| 阳谷县| 云阳县| 吉隆县| 武汉市| 格尔木市| 桓仁| 虎林市| 乾安县|