云自無心水自閑

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

          日歷

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

          隨筆檔案

          積分與排名

          • 積分 - 1127752
          • 排名 - 27

          最新評論

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

          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);
          ????????}

          ????}

          }



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



          主站蜘蛛池模板: 长武县| 乌兰察布市| 靖宇县| 临沧市| 天等县| 海盐县| 剑阁县| 新平| 诸暨市| 丹阳市| 肥城市| 大足县| 双柏县| 宁强县| 商水县| 常宁市| 永顺县| 枣庄市| 潜山县| 凉城县| 吴桥县| 宣威市| 胶南市| 万全县| 高邮市| 永丰县| 彭阳县| 万安县| 措美县| 偏关县| 华亭县| 昌邑市| 毕节市| 南投市| 广饶县| 垦利县| 米易县| 图木舒克市| 蒙城县| 长海县| 临海市|