Willam2004 / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

PowerMockRule not working with AbstractTransactionalJUnit4SpringContextTests #350

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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/fTarge
t/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 reported on code.google.com by zhaoshan...@gmail.com on 13 Oct 2011 at 3:46

GoogleCodeExporter commented 9 years ago
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.  

Original comment by johan.ha...@gmail.com on 21 Oct 2011 at 6:18

GoogleCodeExporter commented 9 years ago
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.

Original comment by jackc...@gmail.com on 21 Mar 2012 at 4:02

GoogleCodeExporter commented 9 years ago
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.

Original comment by jackc...@gmail.com on 22 Mar 2012 at 10:27

GoogleCodeExporter commented 9 years ago
You can try out the Javaagent based PowerMockRule and see if it makes any 
difference.

Original comment by johan.ha...@gmail.com on 26 Mar 2012 at 6:45

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

Original comment by guitarg...@gmail.com on 3 May 2012 at 9:20

GoogleCodeExporter commented 9 years ago
Hi GuitarGice,

This week i try to use the powermockagent 
(http://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.

Original comment by jackc...@gmail.com on 5 May 2012 at 6:06

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Is this issue open yet ? I'm getting the same error =(
Any news ?
Thanks in advance!

Original comment by sandro.c...@gmail.com on 30 Jun 2014 at 9:46

GoogleCodeExporter commented 9 years ago
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 

Original comment by sandro.c...@gmail.com on 30 Jun 2014 at 9:47