praveen1503 / powermock

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

Failed to mock a final class with both @PowerMockIgnore and @PowerMockIgnore set #404

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. import the attached java project into eclipse (the one I use is Juno SR1)
2. select FooUtilsTest and run as Junit Test

What is the expected output? What do you see instead?
Expected behavior: the test passes
Actual behavior: An IllegalArgumentException is thrown as below

java.lang.IllegalArgumentException: Cannot subclass final class class 
foo.FooUtils
    at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)
    at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
    at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
    at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
    at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:94)
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:51)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:100)
    at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
    at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:70)
    at test.FooUtilsTest.testFoo(FooUtilsTest.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:307)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

What version of the product are you using? On what operating system?
OS: Win7 64-bit
JVM: IBM 1.6.0
Eclipse: Juno SR1
PowerMock: 1.4.10

Please provide any additional information below.

To make the test pass, I have to do either of the following:

1. remove line of @PowerMockIgnore("foo.*")
2. change the FooUtils non-final

Original issue reported on code.google.com by shen.bin...@gmail.com on 15 Oct 2012 at 1:48

Attachments:

GoogleCodeExporter commented 8 years ago
You cannot prepare a class that is also ignored. That's the problem.

Original comment by johan.ha...@gmail.com on 6 Nov 2012 at 5:41

GoogleCodeExporter commented 8 years ago
But in the JavaDoc of PowerMockIgnore class, it says clearly,

"Note that the PrepareForTest and PrepareOnlyThisForTest will have precedence 
over this annotation (PowerMockIgnore)."

So I expect if the class appears both in @PrepareForTest in @PowerMockIgnore, 
the class should get mocked.

To give you more background knowledge, I'm actually writing test cases where a 
final class belonging to Eclipse framework need be mocked. But at the same 
time, I have to add @PowerMockIgnore("org.eclipse.*") to not break the 
initialization of host plugin.

BTW, sorry I gave the wrong title, it should be "Failed to mock a final class 
with both @PrepareForTest and @PowerMockIgnore set"

Original comment by shen.bin...@gmail.com on 6 Nov 2012 at 6:12

GoogleCodeExporter commented 8 years ago
Oh I have forgotten about that, sorry. In that case it ought to work I suppose. 
It seem logical that PrepareForTest should have precedence over 
PowerMockIgnore. I'll see if I can reproduce it.

Original comment by johan.ha...@gmail.com on 6 Nov 2012 at 6:31

GoogleCodeExporter commented 8 years ago
Yes this was indeed a bug and I wonder why no one has discovered this before 
(including myself :)).

I've pushed the changes to trunk so please try it out if you like by building 
PowerMock from source (ignore the tests if you're using Java 7). 

Original comment by johan.ha...@gmail.com on 6 Nov 2012 at 7:26

GoogleCodeExporter commented 8 years ago
Cool! I'll give it a try and get back here. Thanks!

Original comment by shen.bin...@gmail.com on 6 Nov 2012 at 7:37

GoogleCodeExporter commented 8 years ago
Verified in latest 1.5 version. Good job!

Original comment by shen.bin...@gmail.com on 14 Dec 2012 at 1:57

GoogleCodeExporter commented 8 years ago
great :)

Original comment by johan.ha...@gmail.com on 14 Dec 2012 at 2:02