【发布时间】:2017-05-02 03:10:58
【问题描述】:
我们的 Spring Web 应用程序的事务性 JUnit 测试失败。
具体来说:如果我分别用 maven 执行每个测试,它们会运行:
mvn -Dtest=Test1
mvn -Dtest=Test2
如果我执行
mvn -Dtest=Test1,Test2
我在其中一项测试中遇到 JPA 错误:
Could not open JPA EntityManager for transaction; nested exception is
java.lang.IllegalStateException:
Attempting to execute an operation on a closed EntityManagerFactory."
type="org.springframework.transaction.CannotCreateTransactionException"
org.springframework.transaction.CannotCreateTransactionException:
Could not open JPA EntityManager for transaction; nested exception is
java.lang.IllegalStateException:
Attempting to execute an operation on a closed EntityManagerFactory.
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.verifyOpen(EntityManagerFactoryDelegate.java:338)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:303)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
at org.springframework.orm.jpa.JpaTransactionManager.createEntityManagerForTransaction(JpaTransactionManager.java:449)
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:369)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:439)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c(AbstractTransactionAspect.aj:64)
...
如果我将surefire插件设置为为每个测试重新创建整个JVM,它们也会运行,这当然会花费大量时间。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.version}</version>
<configuration>
<reuseForks>false</reuseForks>
<forkCount>1</forkCount>
</configuration>
</plugin>
应用设置:
- Spring,带有 Spring Roo(因此 proxymode=asjectJ !)
- Eclipselink 作为 JPA 提供者
用于测试设置的ApplicationContext.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="[packagename]">
<context:exclude-filter expression=".*_Roo_.*" type="regex" />
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
<context:spring-configured />
<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
<context:annotation-config />
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven mode="aspectj"
transaction-manager="transactionManager" />
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" id="dataSource">
<property name="driverClassName" value="${test_database.driverClassName}" />
<property name="url" value="${test_database.url}" />
<property name="username" value="${test_database.username}" />
<property name="password" value="${test_database.password}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="1800000" />
<property name="numTestsPerEvictionRun" value="3" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="validationQuery" value="SELECT 1" />
</bean>
<jdbc:initialize-database data-source="dataSource"
ignore-failures="ALL">
<jdbc:script location="${test_database.selectLocation}" />
<jdbc:script location="${test_database.initLocation}" />
<jdbc:script location="${test_database.dataLocation}" />
</jdbc:initialize-database>
</beans>
JUnit 测试如下所示:
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:META-INF/spring/applicationContext.xml",
"classpath:META-INF/spring/webmvc-config.xml" })
@TransactionConfiguration(transactionManager = "transactionManager")
@DirtiesContext
public class Test1 {
@Autowired
WebApplicationContext wac;
private MockMvc mockMvc;
@PersistenceContext
EntityManager entityManager;
@Before
public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
@Transactional
public void getJSONTest() throws Exception {
...
}
}
有人知道这两个测试是如何连接的吗?一个必须为另一个关闭 entityManager,但为什么以及如何?
感谢任何想法!
Edit1:按照建议删除了@TransactionConfiguration 和@DirtiesContext,但现在我得到了一个
No transaction is currently active; nested exception is
javax.persistence.TransactionRequiredException
Edit2:到目前为止,我一直在追查这个问题,以至于我执行测试的顺序出了问题。因此,测试不是相互独立的。我尝试了几种注释和执行顺序的排列,每次都会导致不同的错误/行为。
目前班级级别的设置是:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:META-INF/spring/applicationContext.xml",
"classpath:META-INF/spring/webmvc-config.xml" })
@WebAppConfiguration
@Transactional
@DirtiesContext
我再次添加了@DirtiesContext 注释,因为新初始化的上下文有时似乎会有所帮助。
这里很难找到真正的问题,但我遇到了一个具体的问题:
我将我的测试类包装在 @Transactional 中,并在我调用的测试方法中
mockMvc.perform(MockMvcRequestBuilders.post(...))
,调用控制器方法。这个控制器方法委托给另一个具有@Transactional 方法的bean。在 mockMvc.perform() 返回后,我正在检查单元测试天气该值已写入数据库。我知道这只有在事务提交到数据库或使用相同的 Transaction/entityManager 时才有效。测试方法的事务,是否与被调用的控制器相同,或者我们在这里谈论的是两个不同的实体管理器/事务?
【问题讨论】:
标签: spring jpa junit aspectj spring-roo