junit学习(十)——Springpring与Hibernate整合的单元测试
程序员文章站
2022-06-23 18:18:18
...
1、在项目中添加Spring x.x Core Libraries和Hibernate x.x Core Libraries,方法与前两篇相同。
需要注意的是:Hibernate最好使用3.3及以上版本。
我本地测试时,Spring2.5版本,Hibernate3.0版本,整合测试时报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/context/CurrentSessionContext at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) at java.security.AccessController.doPrivileged(Native Method) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at com.wjl.junit.SpringAndHibernateTest.setUpBeforeClass(SpringAndHibernateTest.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
原因是:Spring-3.0.5和Hibernate-3.0.5(或者更低)版本的不兼容造成的。Spring-3.0.5需要用到Hibernate中的org/hibernate/context/CurrentSessionContext这个类,但是Hibernate-3.0.5中未提供此类。
解决办法是:把Hibernate升级到3.3以上。
将Hibernate换成3.3之后,问题解决。
2、添加Spring x.x Persistence Core Libraries,方法同前两篇。
也有一点需要注意:在勾选Spring x.x Persistence Core Libraries时,Spring x.x AOP Libraries,也会被勾选而且没法去掉,所以这一步实际上添加了两个Libraries。
3、在Spring的配置文件applicationContext.xml中添加Hibernate的配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- spring管理Hibernate --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> <bean id="date" class="java.util.Date"/> </beans>
4、添加单元测试类进行测试:
package com.wjl.junit; import org.hibernate.impl.SessionFactoryImpl; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Junit_demo_13 * spring与Hibernate整合的单元测试 * **/ public class SpringAndHibernateTest { private static ApplicationContext context = null; @BeforeClass public static void setUpBeforeClass() throws Exception { context = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void test(){ SessionFactoryImpl session = (SessionFactoryImpl)context.getBean("sessionFactory"); System.out.println(session);//结果:org.hibernate.impl.SessionFactoryImpl@1e2befa,说明spring与Hibernate的整合成功 } }
说明spring与Hibernate的整合成功。
上一篇: Jsp账户登录的实现
下一篇: java基础学习二、环境变量配置