Johan,
Thanks for the explanations
The suggestions you made worked fine with the simple representative
classes, but not with the real classes
I then noticed that you have released PowerMock 1.3, so decided to try
that on my simple classes:
MockFinalClass and MockFinalClassTest
I removed the Mockito 1.7 and PowerMock 1.2.5 jars and added only the
PowerMock 1.3 jars (as this contains mockito-all-1.8.0.jar)
- and no other changes
I ran into a couple of problems, and despite reading the Release Notes
for 1.3 in depth cannot resolve them:
1) If I retain @PrepareForTest(MyFinalClassWithStaticMethod.class) in
my test class (MockFinalClassTest), I get the exception:
java.lang.NoClassDefFoundError: my.package.DummyClass$
$EnhancerByMockitoWithCGLIB$$211f4394
at sun.reflect.GeneratedSerializationConstructorAccessor6.newInstance
(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:521)
at
org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance
(SunReflectionFactoryInstantiator.java:40)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy
(ClassImposterizer.java:120)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise
(ClassImposterizer.java:60)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:43)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:39)
at org.mockito.Mockito.mock(Mockito.java:809)
at org.mockito.Mockito.mock(Mockito.java:705)
at my.package.FinalCLassMock.createMockDummyClass(FinalCLassMock.java:
46)
at my.package.FinalCLassMockTest.testCreateMockDummy
(FinalCLassMockTest.java:41)
This exception occurs at the line in FinalClassMock:
mockDummyClass = mock(DummyClass.class);
- this is a Mockito method
This probably relates to the 1.3 Release Notes entry:
Test classes are now always prepared for test automatically. This
means that you can use suppressing constructors and mock final system
classes more easily since you don't have to prepare the actual test
class for test
2) If I comment out @PrepareForTest
(MyFinalClassWithStaticMethod.class) in my test class
(MockFinalClassTest), I get the exception:
java.lang.IllegalArgumentException: Cannot subclass final class class
my.package.MyFinalClassWithStaticMethod
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:102)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise
(ClassImposterizer.java:59)
at
org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl
(MockCreator.java:79)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock
(MockCreator.java:53)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:80)
at my.package.FinalCLassMock.createMockFinalClassWithStaticMethod1
(FinalCLassMock.java:55)
at
my.package.FinalCLassMockTest.testCreateMockFinalClassWithStaticMethod1
(FinalCLassMockTest.java:59)
This exception occurs at the line in FinalClassMock:
PowerMockito.mockStatic(MyFinalClassWithStaticMethod.class);
I have tried the alternatives
PowerMockito.mock(MyFinalClassWithStaticMethod.class);
AND
PowerMockito.spy(MyFinalClassWithStaticMethod.class);
but still get the same exception
Any help or suggestions gratefully received
Thanks
GeoffH
On Oct 4, 4:12 pm, Johan Haleby <
johan.hal...@gmail.com> wrote:
> Ok I've looked at the examples that you provided.
>
> Here are some points:
>
> 1. It seem like you've found a bug in the PowerMock JUnit4 runner when
> you extend from TestCase. The PowerMock runner assumes that you can only
> have a public zero-argument constructor which you must have if you're not
> extending from TestCase (which you should avoid when using JUnit4). So what
> you should do is to just remove the constructor and the test will continue.
> I've added issue
> 174<
http://code.google.com/p/powermock/issues/detail?id=174>that deals
> with this.
> 2. You *must *always use the PowerMockRunner (i.e.
> @RunWith(PowerMockRunner.class)) at the class-level of your test in order to
> make PowerMock tests work. I can see why you removed it because of the
> issues stated in 1 but without it there's no way for PowerMock to work.
> 3. You must also place the PrepareForTest annotation in your test case
> and not in another class. In your example you placed
> @PrepareForTest(MyFinalClassWithStaticMethod.class) and also @RunWith in
> FinalCLassMock, this will *never *work. You must place them in your test,
> FinalCLassMockTest.
> 4. When I changed the test (FinalCLassMockTest) to conform with the
> things stated above the last test
> (testCreateMockFinalClassWithStaticMethod2) still failed. The reason for
> this is that you've treated the static method expectation as a standard
> instance expectation. You had:
>
> mockMyFinalClassWithStaticMethod =
> PowerMockito.mock(MyFinalClassWithStaticMethod.class);
>
> Mockito.when(mockMyFinalClassWithStaticMethod.getDummyClass()).thenReturn(mockDummyClass);
>
> This will not work because "getDummyClass" is a static method but you're
> not mocking the static methods. In order to mock the static method you
> should have done something like:
>
> PowerMockito.mockStatic(MyFinalClassWithStaticMethod.class);
>
> Mockito.when(MyFinalClassWithStaticMethod.getDummyClass()).thenReturn(mockDummyClass);
>
> Remember that you're only mocking instance methods when using
> PowerMockito.mock(..) and only static methods when using
> PowerMockito.mockStatic(..). You should probably refer to the EasyMock
> section of our documentation to understand how things work until we've got
> the Mockito documentation up to date.
>
> Hope this helps,
> /Johan
>
> On Fri, Oct 2, 2009 at 1:14 PM, Johan Haleby <
johan.hal...@gmail.com> wrote:
> > Thank you very much! I'll have a look at this as soon as I can, but it
> > won't be today. Perhaps tomorrow if I find some time.
>
> > /Johan
>
> ...
>
> read more »