看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测试啦。(第一次发表文章在首页,欢迎大家拍砖)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBefor
最新发布
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值