java 使用 Spring、Hibernate 和 Spring 事务支持为单元测试设置数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10970387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setup database for Unit tests with Spring, Hibernate and Spring Transaction Support
提问by mibutec
I want to test integration of dao-layer with my service-layer in a unit-test. So I need to setup some data in my database (hsql). For this setup I need an own transaction at the begining of my testcase to ensure that all my setup is really commited to database before starting my testcase.
我想在单元测试中测试 dao 层与我的服务层的集成。所以我需要在我的数据库(hsql)中设置一些数据。对于这个设置,我需要在我的测试用例开始时有一个自己的事务,以确保在开始我的测试用例之前我的所有设置都真正提交到数据库。
So here's what I want to achieve:
所以这就是我想要实现的目标:
// NotTranactional
public void doTest {
// transaction begins
setup database
// commit transaction
service.doStuff() // doStuff is annotated @Transactional(propagation=Propagation.REQUIRED)
}
Here is my not working code:
这是我不工作的代码:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="org.hsqldb.jdbcDriver"
p:url="jdbc:hsqldb:mem:posmail" p:username="sa"
p:password="" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>de.diandan.asynch.modell.receipt.Position</value>
<value>de.diandan.asynch.modell.receipt.Receipt</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- Enable Hibernate's automatic session context management
<prop key="current_session_context_class">thread</prop> -->
<!-- Disable the second-level cache -->
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
My Testcase
我的测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/asynchUnit.xml"})
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
public class ReceiptServiceTest implements ApplicationContextAware {
@Autowired(required=true)
private UserHome userHome;
private ApplicationContext context;
@Before
@Transactional(propagation=Propagation.REQUIRED)
public void init() throws Exception {
User user = InitialCreator.createUser();
userHome.persist(user);
}
@Test
public void testDoSomething() {
...
}
}
Leading to this exception:
导致此异常:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
at de.diandan.asynch.modell.GenericHome.getSession(GenericHome.java:40)
at de.diandan.asynch.modell.GenericHome.persist(GenericHome.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
at $Proxy28.persist(Unknown Source)
at de.diandan.asynch.service.ReceiptServiceTest.init(ReceiptServiceTest.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access@Before
@Transactional(propagation=Propagation.REQUIRED)
public void setup() {
setup database
}
0(ParentRunner.java:42)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
I dont know whats the right way to get the transaction around setup database.
我不知道在设置数据库周围获取事务的正确方法是什么。
What I tried:
我试过的:
@Transactional(propagation=Propagation.REQUIRED)
public void setup() {
setup database
}
public void doTest {
init();
service.doStuff() // doStuff is annotated @Transactional(propagation=Propagation.REQUIRED)
}
-> Spring seems not to start transaction in @Before-annotated methods. Beyond that, thats not what I really want, cause there are a lot merhods in my testclass which needs a slightly differnt setup, so I need several of that init-methods.
-> Spring 似乎没有在 @Before 注释的方法中启动事务。除此之外,那不是我真正想要的,因为我的测试类中有很多 merhod 需要稍微不同的设置,所以我需要几个 init 方法。
public void doTest {
// doing my own transaction-handling
setup database
// doing my own transaction-handling
service.doStuff() // doStuff is annotated @Transactional(propagation=Propagation.REQUIRED)
}
--> init seems not to get started in transaction
--> init 似乎没有在事务中开始
What I dont want to do:
我不想做什么:
@Transactional(propagation=Propagation.REQUIRED)
public void doTest {
setup database
service.doStuff()
}
--> start mixing springs transaction-handling and my own seems to get pain in the ass.
--> 开始混合 springs 事务处理,我自己的似乎很痛苦。
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
@Transactional
--> I want to test as real as possible situation, so my service should start with a clean session and no transaction opened
--> 我想测试尽可能真实的情况,所以我的服务应该从一个干净的会话开始并且没有打开事务
So whats the right way to setup database for my testcase?
那么为我的测试用例设置数据库的正确方法是什么?
采纳答案by mibutec
Adding the following annotations did the trick:
添加以下注释可以解决问题:
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ConnextConfiguration(locations={"/asynchUnit.xml"})
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
public class ReceiptServiceTest implements ApplicationContextAware {
Here is the resulting full configuration:
这是最终的完整配置:
@RunWith(SpringJUnit4ClassRunner.class)
回答by Francisco Spaeth
For spring oriented tests you can use the spring runner for JUnit:
对于面向弹簧的测试,您可以使用 JUnit 的弹簧运行器:
@Rollback(false)
@Test
public void testProcessWithoutRollback() {
// ...
}
@BeforeTransaction
public void beforeTransaction() {
// logic to be executed before a transaction is started
}
documented here:
记录在这里:
http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch10s03.html
http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch10s03.html
And the part that may interest you is that you are able to annotate methods with annotations as follow:
您可能感兴趣的部分是您可以使用注释对方法进行注释,如下所示:
@AfterTransaction
public void afterTransaction() {
// logic to be executed after a transaction has ended
}
and
和
@NotTransactional
@Test
public void testProcessWithoutTransaction() {
// ...
}
you can even use something like
你甚至可以使用类似的东西
##代码##to indicate you don't wanna transaction applied to it. But be aware of thisdocument with the following warning:
表示您不想将交易应用于它。但请注意本文档有以下警告:
As of Spring 3.0, @NotTransactional is deprecated in favor of moving the non-transactional test method to a separate (non-transactional) test class or to a @BeforeTransaction or @AfterTransaction method. As an alternative to annotating an entire class with @Transactional, consider annotating individual methods with @Transactional; doing so allows a mix of transactional and non-transactional methods in the same test class without the need for using @NotTransactional.
从 Spring 3.0 开始,@NotTransactional 已被弃用,而是将非事务性测试方法移至单独的(非事务性)测试类或@BeforeTransaction 或 @AfterTransaction 方法。作为使用@Transactional 注释整个类的替代方法,考虑使用@Transactional 注释单个方法;这样做允许在同一测试类中混合使用事务和非事务方法,而无需使用 @NotTransactional。