java 单元测试二(hibernate sessionFantory注入)

来源:互联网 发布:js移除所有子元素 编辑:程序博客网 时间:2024/06/05 15:52

利用spring,有时我们需要写的单元测试中需要用到Hibernate的表与实体类对应关系,和sessionFactory的注入方式测试,这时就得读取application的配置文件,下面是个例子:

1、配置文件:applicationContext4Test.xml

<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"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd    http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"><!-- 自动注解 --><context:annotation-config /><!-- 自动扫描的包名 --><context:component-scan base-package="cn.ac.dsp.cms.impl" /><!-- AnnotationMethodHandlerAdapter 通过注解,把一个URL映射到Controller类的方法上--><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean><!-- 默认的注解映射的支持 -->  <mvc:annotation-driven /><!-- 拦截器 --><!--<mvc:interceptors><mvc:interceptor><mvc:mapping path="/**" /><bean class="cn.ac.dsp.cms.impl.MyInterceptor"></bean></mvc:interceptor></mvc:interceptors>--><!-- 对静态资源文件的访问  方案一 (二选一)--><mvc:resources mapping="/fonts/**" location="/views/fonts/" /><mvc:resources mapping="/themes/**" location="/views/themes/" /><mvc:resources mapping="/css/**" location="/views/css/" /><mvc:resources mapping="/js/**" location="/views/js/" /><mvc:resources mapping="/images/**" location="/views/images/" /> <!-- 对静态资源文件的访问  方案二(二选一) --> <mvc:default-servlet-handler /><!-- 引入属性文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location"><value>classpath:c3p0.properties</value></property></bean><!-- 数据库连接数据源的配置 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="autoCommitOnClose" value="true" /><property name="checkoutTimeout" value="${c3p0.checkoutTimeout}" /><property name="initialPoolSize" value="${c3p0.minPoolSize}" /><property name="minPoolSize" value="${c3p0.minPoolSize}" /><property name="maxPoolSize" value="${c3p0.maxPoolSize}" /><property name="maxIdleTime" value="${c3p0.maxIdleTime}" /><property name="acquireIncrement" value="${c3p0.acquireIncrement}" /><property name="maxIdleTimeExcessConnections" value="${c3p0.maxIdleTimeExcessConnections}" /></bean><!-- 配置sessionFactory,和包的自动扫描 --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop><prop key="hibernate.hbm2ddl.auto">update</prop><prop key="show_sql">true</prop><prop key="format_sql">true</prop></props></property><property name="packagesToScan"><list><value>cn.ac.dsp.cms.domain</value><value>cn.ac.dsp.cms.multisource.bean</value></list></property></bean><!-- 事务的处理 --><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:annotation-driven transaction-manager="transactionManager" /><!-- bean组件 --><bean id="<span style="font-family: Arial, Helvetica, sans-serif;">testService</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span>class="cn.ac.dsp.cms.impl.<span style="font-family: Arial, Helvetica, sans-serif;">UnitTest</span><span style="font-family: Arial, Helvetica, sans-serif;">"></span><property name="sessionFactory" ref="sessionFactory" /></bean></beans>

2、测试单元类
package cn.ac.dsp.cms.test;import junit.framework.TestCase;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.ac.dsp.cms.IUrlParserService;public class UnitTest extends TestCase {private IUrlParserService testService = null;@Beforepublic void setUp() throws Exception {//读取配置文件ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext4Test.xml");  testService =  ctx.getBean("testService");}@Afterpublic void tearDown() throws Exception {}@Testpublic void testUrlCount() {//测试逻辑代码testService.add();}}

3、右击运行即可

0 0