Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerMockRule not working with AbstractTransactionalJUnit4SpringContextTests #370

Open
johanhaleby opened this issue Jul 24, 2015 · 8 comments

Comments

@johanhaleby
Copy link
Collaborator

From zhaoshan...@gmail.com on October 13, 2011 17:46:33

I'm getting an exception when using PowerMockRule for a test class extending from AbstractTransactionalJUnit4SpringContextTests.

This test works:

@ContextConfiguration(locations="/meta-inf/springtest/applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@PrepareForTest({Utils2.class})
public class TempTest {
@rule
public PowerMockRule rule = new PowerMockRule ();

@Autowired
private Utils2 utils;

@Test
public void say() {
    Assert.assertNotNull(this.utils);
}

}

This doesn't:

@ContextConfiguration(locations="/meta-inf/springtest/applicationContext.xml")
@PrepareForTest({Utils2.class})
public class TempTest extends AbstractTransactionalJUnit4SpringContextTests {

@Rule         
public PowerMockRule rule = new PowerMockRule ();

@Autowired
private Utils2 utils;

//@Autowired
// private NeighborhoodService neighborhoodService;

@Test
public void say() {
    // Assert.assertNotNull(this.neighborhoodService);
    Assert.assertNotNull(this.utils);
}

}

I got an exception with this stack trace:

com.thoughtworks.xstream.converters.ConversionException: javassist.NotFoundException: $Proxy4 : javassist.NotFoundException: $Proxy4
---- Debugging information ----
message : javassist.NotFoundException: $Proxy4
cause-exception : java.lang.RuntimeException
cause-message : javassist.NotFoundException: $Proxy4
class : java.lang.Class
required-type : java.lang.Class
converter-type : com.thoughtworks.xstream.converters.SingleValueConverterWrapper
wrapped-converter : com.thoughtworks.xstream.converters.extended.JavaClassConverter
path : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/fTarget/applicationContext/classLoader/classes/java-class[53]
line number : 1455
class[1] : java.util.Vector
converter-type[1] : com.thoughtworks.xstream.converters.collections.CollectionConverter
class[2] : sun.misc.Launcher$AppClassLoader
converter-type[2] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[3] : org.springframework.context.support.GenericApplicationContext
class[4] : foo.service.TempTest
class[5] : org.junit.internal.runners.statements.InvokeMethod
class[6] : org.powermock.modules.junit4.rule.PowerMockStatement
class[7] : org.powermock.modules.junit4.rule.PowerMockStatement$1
version : null

...
at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:54)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate( PowerMockRule .java:49)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)

I'm using PowerMock 1.4.10, Javassist 3.15.0.GA, and XStream 1.4.1 on Windows 7.

I also tried not extending AbstractTransactionalJUnit4SpringContextTests, but autowiring an entity object. I got a similar exception on not finding a proxy class (CGLIB enhanced).

It looks like PowerMockRule doesn't work with proxy objects.

Thanks!

Original issue: http://code.google.com/p/powermock/issues/detail?id=350

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on October 20, 2011 23:18:57

Thanks for reporting. The behavior of the PowerMockRule is quite complex and it's hard to cover all the corner-cases. Perhaps what you need to do is to ignore some packages for test that you suspect are creating the proxies (if possible). If not I'm afraid you would have to help us out a bit and dig into the PowerMock code and try to find a solution to the problem.

@johanhaleby
Copy link
Collaborator Author

From jackc...@gmail.com on March 21, 2012 09:02:42

Hi,

I have this issue too and i understand why:

At some moment PowerMock serialize the test class and Xstream cannot serialize the ApplicationContext class from Spring(Proxy classe too and some others from my experience.)

I try to make a patch but i do not understand why PowerMock deep clone some classes. If you can explain me that, i think i could find a patch.

Thanks for your help.

@johanhaleby
Copy link
Collaborator Author

From jackc...@gmail.com on March 22, 2012 03:27:23

Hi Again,

After understood that the problem is about Serialization. I add the key-word transient on attribute applicationContext and I run again the test to discover that my test case is executed from the tests class cloned and by consequence attribute applicationContext is null.

Can you explain me why tests cases is executed from the tests class cloned ?

Thanks in advance.

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on March 25, 2012 23:45:00

You can try out the Javaagent based PowerMockRule and see if it makes any difference.

@johanhaleby
Copy link
Collaborator Author

From guitarg...@gmail.com on May 03, 2012 14:20:39

Jack,
Did you ever get around this issue?
I am having the exact same problem.
Thanks in advance.

@johanhaleby
Copy link
Collaborator Author

From jackc...@gmail.com on May 05, 2012 11:06:15

Hi GuitarGice,

This week i try to use the powermockagent ( https://code.google.com/p/powermock/wiki/PowerMockAgent ) and it solved my problem.

Indeed my issue was that serialization has tried to serialize a huge object in order to change where my huge instance is loaded. With powermockagent no more serialization is needed by powermock to inject some source code and do its job.

The only constraint to use this agent is its demand some configuration. And if you use eclipse + maven you have to configured eclipse to use the agent and maven too. if you forgot one of them some tests will fail.

@johanhaleby
Copy link
Collaborator Author

From sandro.c...@gmail.com on June 30, 2014 14:46:38

Is this issue open yet ? I'm getting the same error =(
Any news ?
Thanks in advance!

@johanhaleby
Copy link
Collaborator Author

From sandro.c...@gmail.com on June 30, 2014 14:47:54

In my case i'm using:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({ "classpath:/WEB-INF/servlet.xml", "classpath:/WEB-INF/security.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@PrepareForTest({ AccessLoginWebDatabase.class, AccessLoginWebDatabase.class })
public class AuthenticationControllerTestCase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant