云自无心水自闲

天平山上白云泉,云自无心水自闲。何必奔冲山下去,更添波浪向人间!
posts - 288, comments - 524, trackbacks - 0, articles - 6
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

appfuse 1.9.4 学习心得 -创建新的Manager

Posted on 2007-03-14 15:09 云自无心水自闲 阅读(760) 评论(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实例的。




只有注册用户登录后才能发表评论。


网站导航: