33

当尝试使用PowerMockRule 技术组合 Spring 测试运行器和 PowerMock 运行器时,每当我尝试EntityManager使用PersistenceContextJPA 的注释注入一个时,我都会从 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 上的完整回溯的链接,回溯超过了问题长度的限制):

com.thoughtworks.xstream.converters.ConversionException: Could not call org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.readObject() : Could not call java.util.concurrent.CopyOnWriteArrayList.readObject() : Could not call java.security.CodeSource.readObject() : null

相关持久化单元:

<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 组合的一些其他错误,但不是这个。

4

1 回答 1

1

我遇到了类似的问题,有人建议我创建一个 Resources.java 生成实体管理器的类。

@Stateful
public class Resources implements Serializable {

    @PersistenceContext (type = PersistenceContextType.TRANSACTION)
    private EntityManager entityManager;

    @Produces
    public EntityManager getEntityManager() {
        return entityManager;
    }

}

我仍然不确定为什么这个解决方案对我有用……但确实如此。

v/r 王牌

于 2013-12-11T17:09:44.060 回答