当尝试使用PowerMockRule 技术组合 Spring 测试运行器和 PowerMock 运行器时,每当我尝试EntityManager
使用PersistenceContext
JPA 的注释注入一个时,我都会从 Thoughtworks XStream 库中得到一个异常。不使用PowerMockRule
. 在测试开始时,我也忽略了 PowerMockLoader 中的所有包。我尝试了各种值,@PowerMockIgnore
因为这通常可以解决我在 PowerMock 上遇到的问题,但是,即使完全忽略每个包,这个错误仍然会发生。
@PowerMockIgnore("*")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/test-configuration.xml")
public class SpringAndPowerMockTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@PersistenceContext
private EntityManager manager;
@Test
public void test() {
}
}
例外情况如下 - 指向 pastebin 上的完整回溯的链接,回溯超过了问题长度的限制):
相关持久化单元:
<persistence-unit name="inMemory">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:mem:InMemoryUnitTests;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
Maven依赖:
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
使用 Spring 进行测试3.2.3.RELEASE
。请注意,我还测试了 spring-test 版本3.0.5.RELEASE
,它确实解决了 Spring/Powermock 组合的一些其他错误,但不是这个。