[英]Integration testing Spring repository layer with DBUnit
我一直在關注Petri Kainulainen的這篇非常有用的博客文章,使用spring-data-jpa和DBUnit為我的Spring存儲庫代碼編寫集成測試。
我有這樣的bean上下文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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:property-placeholder location="file:src/main/webapp/WEB-INF/config/application-test.properties"
ignore-resource-not-found="false" />
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource">
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="directorRepository" name="directorRepositoryMock" class="org.mockito.Mockito"
factory-method="mock">
<constructor-arg value="com.tvfreakz.repository.DirectorRepository" />
</bean>
</beans>
我的數據集XML具有以下內容
<!DOCTYPE dataset SYSTEM "my-dataset.dtd">
<dataset>
<director directorid="1" director="Ridley Scott" />
<director directorid="2" director="James Cameron" />
<director directorid="3" director="David Fincher" />
<director directorid="4" director="Jean-Pierre Jeunet" />
</dataset>
這是存儲庫界面
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.tvfreakz.model.entity.Director;
@Repository("directorRepository")
public interface DirectorRepository extends JpaRepository<Director, Long> {
Director findByDirectorId(Long directorId);
}
我的測試類就是這樣設置的
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.tvfreakz.model.entity.Director;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/config/testMVCContext.xml"})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
@DatabaseSetup("testDataset.xml")
public class ITDirectorRepositoryTest {
@Autowired
private DirectorRepository repository;
@Test
public void testFindByDirectorIdWhenNoDirectorFound() {
Director director = repository.findByDirectorId(10L);
assertNull(director);
}
@Test
public void testFindByDirectorIdWhendirectorIsFound() {
Director director = repository.findByDirectorId(1L);
assertEquals("Ridley Scott", director.getDirectorName());
}
}
我期待@DatabaseSetup
注釋采用我的配置並將所有表和數據插入到我的由dataSource
bean配置的測試數據庫中。
這似乎什么也沒做(即數據庫保持空白,沒有表或數據),我的第二個測試testFindByDirectorIdWhendirectorIsFound
失敗,因為導致返回的是null。 我錯過了什么?
編輯1好我有一些進展,我現在有表創建,但沒有數據。 問題是我不應該使用模擬存儲庫。 我不得不將我的上下文XML更新為以下內容。 因此,下一個問題是嘗試找出為什么它沒有用數據填充我新創建的表
編輯2 JpaTransactionManager bean和tx注釋驅動添加到上下文
<?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:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:property-placeholder location="file:src/main/webapp/WEB-INF/config/application-test.properties"
ignore-resource-not-found="false" />
<jpa:repositories base-package="com.tvfreakz.repository" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="punit"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<entry key="hibernate.hbm2ddl.auto" value="update"/>
<entry key="hibernate.format_sql" value="true"/>
<entry key="hibernate.show_sql" value="true"/>
</map>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource">
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
</beans>
編輯3確定在我的maven依賴項中包含slf4j並創建了一個設置了DEBUG級別的log4j.properties文件后,我設法啟用了DEBUG級別。
我在日志中看到了這一點,這或許給出了一個線索?
14:47:05,670 INFO TestContextManager:242 - Could not instantiate TestExecutionListener [com.github.springtestdbunit.DbUnitTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/dbunit/dataset/IDataSet]
我相信這是參考我測試代碼的這一部分
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
隨着我對原始帖子的編輯,似乎我在Maven pom.xml中缺少DBUnit依賴項。 添加以下內容后,我現在有了數據,感謝Petri和Phil的幫助!
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.0</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.