在用Spring框架开发过程中,会定义一些bean。有些bean之间有依赖关系。
单元测试(Unit Test)需要构造被测试的对象,有些可以通过简单的new运算符生成一个实例。对于比较复杂的对象,比如DAO,有一些依赖关系,构造起来比较麻烦。这时可以通过Spring Test Framework的annotation机制来处理这些复杂的对象。
假设文件路径如下:
<Project>/src/com/example/dao
<Project>/test/com/example/dao
package com.example.dao
@ContextConfiguration(
locations={"classpath:MyDAOTest-context.xml",
"classpath:other-context-config.xml"
}
)
public class MyDAOTest extends AbstractJUnit4SpringContextTests {
// @Resource(name="myDao")
@Autowired
private MyDAO myDao;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testSave() {
MyBean entity = new MyBean();
//set attributes
myDao.saveOrUpdate(entity);
Assert.isTrue(true);
}
}
put MyDAOTest-context.xml, other-context-config.xml under folder <Project>/test
------------------------------MyDAOTest-context.xml-------------------------------------------
<?xml version="1.0" encoding="UTF-8"?> <!--<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="myDao" class="com.example.dao.MyDAOImpl"> <property name="sessionFactory" ref="amSessionFactory"/> </bean> <bean id="amSessionFactory" parent="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- <property name="packagesToScan"> --> <!-- <list> --> <!-- <value>com.example.model</value> --> <!-- </list> --> <!-- </property> --> <property name="packagesToScan" value="com.example.model" /> </bean> </beans>
-------------------------------------------------other-context-config.xm-------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?> <!--<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:database.properties" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${oracle.jdbc.driver_class}"/> <property name="url" value="${oracle.jdbc.url}"/> <property name="username" value="${oracle.jdbc.username}"/> <property name="password" value="${oracle.jdbc.password}"/> </bean> <bean id="entityInterceptor" class="com.mercury.itg.core.persistency.HibernateITGInterceptor"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <ref bean="hibernateProperties"/> </property> <property name="entityInterceptor"> <ref local="entityInterceptor"/> </property> </bean> <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.c3p0.minPoolSize">5</prop> <prop key="hibernate.c3p0.maxPoolSize">20</prop> <prop key="hibernate.c3p0.timeout">600</prop> <prop key="hibernate.c3p0.max_statement">50</prop> <prop key="hibernate.c3p0.testConnectionOnCheckout"> false </prop> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> </bean> <tx:annotation-driven/> </beans>
--------------------------------------database.properties-------------------------------------------
oracle.jdbc.driver_class=oracle.jdbc.driver.OracleDriver
oracle.jdbc.url=jdbc:oracle:thin:@localhost:1521:MySID
oracle.jdbc.username=username
oracle.jdbc.password=password
oracle.jdbc.schema=MySchema
oracle.jdbc.system.username=system
oracle.jdbc.system.password=Admin2012
oracle.jdbc.sys.username=sys
oracle.jdbc.sys.password=Admin2012
@ContextConfiguration 究竟做了什么呢?可以通过下面这段代码来解释。
public class MyDAOTest {
protected static ApplicationContext ac = null;
static{
ac = new ClassPathXmlApplicationContext(new String[]{"MyDAOTest-context.xml","other-context-config.xml"});
}
protected MyDAO myDao;
@Before
public void setUp() throws Exception {
myDao = (MappingDAO)ac.getBean("myDao");
}
@After
public void tearDown() throws Exception {
}
@Test
public void testSave() {
...
}