论坛首页 入门技术论坛

junit 测试 spring+hibernate

浏览 2839 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-05-16  

这是很不标准的测试用例呵呵
注意这种方法是测试 service层和 dao层同时测试
这时会出现问题 org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
需要把 相关的表设置为  lazy="false" 测试完后别忘了设置回初始状态啊,呵呵
而且需要把 加载 hiberante 的hibernate.cfg.xml 文件的地方 也要像下面一样 配置成绝对路径

public class MessageTestCase {

    ApplicationContext ap;
   
    @Before
    public void setUp() throws Exception {
        String[] config={"file:///home/hgh/project/java/cnbrn/root/WEB-INF/config/applicationContext.xml",
                "file:///home/hgh/project/java/cnbrn/root/WEB-INF/config/applicationContext-bussiness.xml",
            "file:///home/hgh/project/java/cnbrn/root/WEB-INF/config/applicationContext-dao.xml",
            "file:///home/hgh/project/java/cnbrn/root/WEB-INF/config/applicationContext-datasource.xml",
            "file:///home/hgh/project/java/cnbrn/root/WEB-INF/config/applicationContext-hibernate.xml",
            "file:///home/hgh/project/java/cnbrn/root/WEB-INF/config/applicationContext.xml"
                       
                       
                };

        ConstantVar.ROOT_PATH="/opt/webserver/apache-tomcat-5.5.23/webapps/cnbrn/";
                ap=new FileSystemXmlApplicationContext(config);
    }


//    @Test
    public void testPassMessage() {
        MessageService messageService=(MessageService)ap.getBean("messageService");
       
        BaseMessagePass passMsg=new BaseMessagePass();
        passMsg.setMessageTitle("测试传递1");
        passMsg.setMessageBody("测试传递1");
        passMsg.setMessageType(MessageConstant.PASS_MESSAGE_TYPE_IN_SITE);
        Calendar c=Calendar.getInstance();
        passMsg.setSendTime(new Date());
        c.setTimeInMillis(((c.getTimeInMillis()/1000)+(24*60*60))*1000);
        passMsg.setEndDate(c.getTime());
        passMsg.setSendDepth(5);
        passMsg.setIsEffect(MessageConstant.PASS_MESSAGE_STATUS_ALIVE);
        Tuser tuser=new Tuser();
        tuser.setId("5dfabc7a0fd83ad8010fd83c02b10006");
       
        try {
            messageService.sendBaseMessagePass(tuser, passMsg);
        } catch (CnbrnException e) {
            e.printStackTrace();
        }
       
    }

    @Test
    public void testSendBaseMessagePass() {
        Tuser tuser=new Tuser();
        tuser.setId("5dfabc7a0fd87c55010fd92d180d0178");
        tuser.setFirstName("啊");
        tuser.setLastName("不");
        MessageService messageService=(MessageService)ap.getBean("messageService");
        BaseMessagePass passMsg =(BaseMessagePass)messageService.select(BaseMessagePass.class, "ff8081811294359701129435ace60001");
        try {
            messageService.passMessage(tuser, passMsg);
        } catch (MessageErrorException e) {
            e.printStackTrace();
        }
    }

}
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics