看Spring源码,配置自己的JUnit Test.

在我同学老欧的影响下,我也认真学习了一下Spring的源码,学习了一下mock包,并总结搞出了自己的JUnit Test,Spring让一切如此简单。
我自己实际写的比下面的更简单,不需要像下面的JUnitConfigBase.xml和JUnitConfig.xml那么繁琐,但是加上他后文章更直观。我实际写的的只是改写了下面JUnitTestJavaeye.java加载自己工程的config mapping,就不需要下面两个xml文件了.这里就不多说了,希望大家看了后也能改动他,让他更简单。

1 导入一个spring-mock.jar;
2 这里需要写你的JUnit配置文件
JUnitConfig.xml
主要是你要测试的类的配置。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!-- 下面beans是准备被测试的 -->
<bean id="itemService" class="com.cme.javaeye.item.service.ItemServiceImpl">
<property name="itemDao" ref="itemDao"/>
</bean>

<bean name="itemDao" class="com.cme.javaeye.item.dao.itemDaoImpl" parent="baseHibDao"/>

<bean id="itemController" class="com.cme.javaeye.item.controller.ItemController">
<property name="itemService" ref="itemService"/>
</bean>

</beans>



JUnitConfigBase.xml
主要是数据源和hibernate的配置。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!--
- Application context definition for Petclinic on Hibernate.
-->
<beans>

<!-- Configure Local DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/myDB?autoReconnect=true</value></property>
<property name="username"><value>username</value></property>
<property name="password"><value>password</value></property>
</bean>

<!-- Configure Base And Templates Of Hibernate -->
<bean id="hibSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="exposeTransactionAwareSessionFactory"><value>false</value></property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="hibSessionFactory" />
</bean>

<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor" >
<property name="sessionFactory" ref="hibSessionFactory" />
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="hibSessionFactory" />
</bean>

<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
<property name="transactionManager" ref="transactionManager" />
</bean>

<bean id="baseHibDao" abstract="true">
<property name="sessionFactory" ref="hibSessionFactory"/>
</bean>

</beans>

3 写一个JUnit的基类 JUnitTestJavaeye.java.
它继承了一个抽象类,并重写了他继承的类的父类AbstractSingleSpringContextTests的getConfigLocations()方法。

package com.javaeye.junittest;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

public abstract class JUnitTestJavaeye extends AbstractDependencyInjectionSpringContextTests {
protected String[] getConfigLocations() {
String[] config = new String[] {
"classpath:JUnitConfigBase.xml","classpath:JUnitConfig.xml"
};

return config;
}
}

4 一切准备就绪啦,大家都看到啦JUnitTestJavaeye.java是一个抽象类,当然是被继承的。
我们写的测试类只要继承他就OK啦。

package com.cme.junittest;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import com.cme.javaeye.item.controller.ItemController;
import com.cme.javaeye.item.form.ItemForm;

public class TestJUnit extends JUnitTestJavaeye {
private ItemController controller;

public void testInsert() throws Exception {

controller = (ItemController) this.applicationContext.getBean("itemController");
ItemForm form = new ItemForm();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();

form.setItemId("1");
form.setDateAdded("2007-07-07");
form.setDateLastModified("2007-07-07");
//more...
//测试你的itemController的save方法.
controller.saveItem(request, response, form);

}

}

现在你就可以用JUnit测试啦。(第一次发表文章在首页,欢迎大家拍砖)
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页