将基于Spring的UnitTest集成到Maven中

在进行persistence layer的开发时,我们总是要写写UnitTest来验证我们提供给上一层的API是经过验证了的。通常我们需要单独的为UnitTest编写一份独立的Spring ApplicationContext配置文件,基于Maven约定大于配置的实践,我们将Spring ApplicationContext配置文件放在项目的src/test/resources下,Test Source Code放在sr/test/java下。

Spring为我们提供一个非常方便的Annotation类@ContextConfiguration,可以使用该Annotation告诉Spring将在何处载入ApplicationContext配置文件以便运行测试代码。将此Annotation配置在UnitTest类声明上,则UnitTest类就可方便地利用Spring来编写测试用例了。

下面为一UnitTest实例:

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/forum-repository-test.xml" })
public class JpaUserRepositoryTest {

	@Autowired
	private UserRepository userRepository;

	@Test
	public void testGetUserByName() {
		String name = "Wang lu";
		User user = userRepository.getUserByName(name);

		Assert.assertEquals(name, user.getUserName());
	}
}


如果在运行Unitest中需要loadtimeweaver功能(比如你的ORM框架使用的是eclipselink),则在Maven的pom中需要添加如下plugin并指定argLine,将javaagent指向spring-agent.jar。

<build>

    …

    <plugins>

     …

      </plugin>

      <plugin>

        <artifactId>maven-surefire-plugin</artifactId>

        <version>2.10</version>

       <configuration>

        <forkMode>once</forkMode>

        <argLine>

             -javaagent:{spring-agent.jar path}

           </argLine>

           <useSystemClassloader>true</useSystemClassloader>

       </configuration>

      </plugin>

    </plugins>

  </build>


以下为Spring ApplicationContext测试配置文件:

<?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:context="http://www.springframework.org/schema/context"

    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:component-scan base-package="com.lulusoft.forum.infrastructure.repositories"></context:component-scan>

 

 

    <bean id="frmDataSource" class="org.apache.commons.dbcp.BasicDataSource"

       destroy-method="close" p:driverClassName="oracle.jdbc.driver.OracleDriver"

       p:url="jdbc:oracle:thin:@//xiechangming:1521/xiech2" p:username="htl_owner"

       p:password="htlowner" />

 

    <bean id="forumEntityManagerFactory"

       class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

       <property name="persistenceUnitName" value="forumPU" />

       <property name="dataSource" ref="frmDataSource" />

       <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />

       <property name="loadTimeWeaver">

           <bean

               class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />

       </property>

       <property name="jpaVendorAdapter">

           <bean

              class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">

              <property name="generateDdl" value="false" />

              <property name="database" value="ORACLE" />

              <property name="databasePlatform"

                  value="org.eclipse.persistence.platform.database.oracle.Oracle11Platform" />

           </bean>

       </property>

    </bean>

</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值