我想为DAO编写单元测试。但在我们的项目中,我们有超过200个DAO和200个模型类。我们使用jpaRepository来编写DAO。但是当我进行单元测试时,我遇到了问题。我花了很多时间来创建应用程序上下文。我有个主意。我只创建一个我要测试的bean DAO而不是创建所有DAO。但是jpa的DAO:repository是接口,所以我不能用class创建一个bean接口。如果创建一个扩展jpa的bean:reponsitories是不可能的,你能否建议我更快地运行单元的其他方法。 我的单元测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/spring/testContext.xml"})
@TransactionConfiguration(defaultRollback = true)
// Importance, as the transaction will be rollback for each test
// give us a clean state.
@Transactional
public class CommunicationDetailDAOTest
{
@Autowired
CommunicationDetailDAO communicationDetailDAO;
@Test
public void testSomeThing()
{
System.out.println("helloworld");
}
}
file testContext.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:META-INF/spring/properties/prism_local_persistence_test.properties"/>
</bean>
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="/META-INF/persistence.xml"/>
<property name="persistenceUnitName" value="mobilePersistenceUnit"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
<!--using postgres driver-->
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"></property>
<property name="entityManagerFactory" ref="entityManager"/>
</bean>
<!-- Spring Data Jpa setup -->
<jpa:repositories
base-package="com.discorp.model.dao"
entity-manager-factory-ref="entityManager"
transaction-manager-ref="transactionManager"></jpa:repositories>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--<context:component-scan base-package="com.qsoft"/>-->
</beans>
和文件persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="mobilePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.discorp.model.common.Address</class>
<class>com.discorp.model.common.Application</class>
.... more than 200 model classes
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.show_sql" value="false"/>
<!--<property name="hibernate." value="false"/> -->
</properties>
</persistence-unit>
</persistence>
答案 0 :(得分:0)
为不同的测试制作不同的配置文件,仅包括测试所需的dao:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:repository="http://www.springframework.org/schema/data/repository"
...
xsi:schemaLocation="http://www.springframework.org/schema/beans
...
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository.xsd
...">
...
<jpa:repositories base-package="path.to.your" >
<repository:include-filter type="assignable" expression="path.to.your.Repository"/>
</jpa:repositories>