0

我想为 DAO 编写单元测试。但是在我们的项目中,我们有超过 200 个 DAO 和 200 个模型类。我们使用 jpaRepository 来编写 DAO。但是我在运行单元测试时遇到了问题。我花了很多时间来创建应用程序上下文。我有个主意。我只创建一个我想测试的 bean DAO,而不是创建所有 DAO。但是 jpa:repository 的 DAO 是接口,所以我不能创建一个类是接口的 bean。如果创建扩展 jpa:reponsitories 的 bean 是不可能的,你能建议我其他更快地运行单元的方法吗?我的单元测试:

@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");
    }
}

文件 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>
4

1 回答 1

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>

于 2013-09-16T18:14:34.977 回答