Powermock 和 Spring 在测试中注入 EntityManager 时引起转换异常

2022-09-01 09:06:20

当尝试使用PowerMockRule技术将Spring测试运行程序和PowerMock运行程序组合在一起时,每当我尝试使用JPA中的注释注入注释时,我都会从Thoughtworks XStream库中获得异常。不使用 .在测试开始时,我还忽略了PowerMockLoader中的所有软件包。我尝试了各种值,因为这通常可以解决我在PowerMock上遇到的问题,但是,即使完全忽略每个包,此错误仍然会发生。EntityManagerPersistenceContextPowerMockRule@PowerMockIgnore

@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:

<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 / Powermock组合的其他一些错误,但不是这个错误。3.2.3.RELEASE3.0.5.RELEASE


答案 1

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

@Stateful
public class Resources implements Serializable {

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

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

}

我仍然不完全确定为什么这个解决方案对我有用...但它做到了。

v/r 王牌


答案 2

推荐