Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powermock and Spring cause ConversionException when injecting EntityManager in test

When attempting to combine the Spring test runner and the PowerMock runner using the PowerMockRule technique, I get an exception from the Thoughtworks XStream library whenever I try to inject an EntityManager using the PersistenceContext annotation from JPA. The same test works fine when not using the PowerMockRule. I also ignore all packages from the PowerMockLoader at the start of the test. I tried various values for @PowerMockIgnore as this normally solves issues I have with PowerMock, however, this error still happens even when ignoring absolutely every package.

@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() {     } } 

The exception is as follows -- links to full backtrace on pastebin the backtrace exceeded the limit for question length):

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

Relevant persistence unit:

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

Testing using Spring 3.2.3.RELEASE. Note that I have also tested with spring-test version 3.0.5.RELEASE which does indeed resolve some other errors with the Spring/Powermock combination, but not this one.

like image 291
amoe Avatar asked Jul 15 '13 15:07

amoe


1 Answers

I had a similar issue and it was suggested to me to create a Resources.java class that Produced an Entity Manager.

@Stateful public class Resources implements Serializable {      @PersistenceContext (type = PersistenceContextType.TRANSACTION)     private EntityManager entityManager;      @Produces     public EntityManager getEntityManager() {         return entityManager;     }  } 

I'm still not exactly sure why this solution worked for me... but it did.

v/r Ace

like image 138
AceFunk Avatar answered Oct 02 '22 23:10

AceFunk