Re: Using PowerMockito to mock static methods in a final class

4,625 views
Skip to first unread message
Message has been deleted
Message has been deleted

GeoffH

unread,
Oct 1, 2009, 7:29:45 AM10/1/09
to PowerMock
Im using
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyFinalClassWithStaticMethod.class)
at the class level

Because of the security constraints where I am working, I am unable to
check out the trunk, so had to go with version 1.2.5
I've looked at the release notes and the new version may solve my
problem
Is there any other way I can get the new version

Thanks

GeoffH


On Oct 1, 11:51 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> Are you using @RunWith(PowerMockRunner.class) and preparing the correct
> classes for test using the PrepareForTest annotation
> (MyFinalClassWithStaticMethod.class)?
>
> We're about to release a new version of PowerMock very soon with a much
> improved Mockito (1.8) support. If you like, feel free to check out
> PowerMock from trunk and use that version instead.
>
> /Johan
>
> On Thu, Oct 1, 2009 at 11:38 AM, GeoffH <geoffhartn...@gmail.com> wrote:
>
> > Hello,
>
> > I'm having trouble using PowerMockito to mock static methods in a
> > class that has been declared as final
> > I'm using PowerMock 1.2.5 with Mockito 1.7
>
> > My first attempt was:
> >  PowerMockito.mockStatic(MyFinalClassWithStaticMethod.class);
> >  Mockito.when(MyFinalClassWithStaticMethod.staticMethod()).thenReturn
> > (someMock);
> >  - where someMock has been previously created
>
> > This threw the following exception:
> > org.mockito.exceptions.misusing.MissingMethodInvocationException:
> > when() requires an argument which has to be a method call on a mock.
> > For example:
> >    when(mock.getArticles()).thenReturn(articles);
> > Also make sure the method is not final - you cannot stub final
> > methods.
>
> > Second attempt (to circumvent the above) was:
> > MyFinalClassWithStaticMethod mock = PowerMockito.mock
> > (MyFinalClassWithStaticMethod.class);
>
> > This threw the following exception:
> > java.lang.IllegalArgumentException: Cannot subclass final class class
> > MyFinalClassWithStaticMethod
> >        at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
> >        at org.mockito.cglib.core.DefaultGeneratorStrategy.generate
> > (DefaultGeneratorStrategy.java:25)
> >        at org.mockito.cglib.core.AbstractClassGenerator.create
> > (AbstractClassGenerator.java:216)
> >        at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
> >        at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
> >        at
> > org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass
> > (ClassImposterizer.java:99)
> >        at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise
> > (ClassImposterizer.java:57)
> >        at
> > org.powermock.api.mockito.PowerMockito.createMethodInvocationControl
> > (PowerMockito.java:240)
> >        at org.powermock.api.mockito.PowerMockito.doMock(PowerMockito.java:
> > 155)
> >        at
> > org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:111)
>
> > Any help on this would be much appreciated
> > It's quite possible that I have the syntax wrong, so an example would
> > be useful
> > Thanks for reading

Johan Haleby

unread,
Oct 1, 2009, 7:36:18 AM10/1/09
to powe...@googlegroups.com
I don't know of any other way unfortunately. Hopefully I'll get to release a new version next week.

You could have a look at http://code.google.com/p/powermock/source/browse/tags/powermock-1.2.5/modules/module-test/powermockito/junit4/src/test/java/samples/powermockito/junit4/staticmocking/MockStaticTest.java to see an example of how you can mock static methods with Mockito and PowerMock 1.2.5. Note though that the syntax will change (slightly) in version 1.3. In 1.3 you now longer pass the class to the "verifyStatic" method.

/Johan
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

GeoffH

unread,
Oct 2, 2009, 4:25:18 AM10/2/09
to PowerMock
Yohan,

I have created minimal representative classes and uploaded them to the
file upload area:
MyFinalClassWithStaticMethod.java is the final class with a static
method
DummyClass is what is returned in the static method in
MyFinalClassWithStaticMethod.java
FinalClassMock.java attempts to create mocks of
MyFinalClassWithStaticMethod and DummyClass
FinalClassMockTest.java is a JUnit test for FinalClassMock.java

FinalClassMockTest.java runs the three tests, and passes the test for
creating a mock of DummyClass as expected
The second and third test throw exceptions as described

It would be great if you could have a look and any feedback would be
welcome
- even if it is the case that I am using PowerMock / Mockito
incorrectly

Thanks for your time

GeoffH

On Oct 1, 4:51 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> That would do just fine. Unfortunately I may not have time to look at it
> until later this weekend though.
>
> /Johan
>
> On Thu, Oct 1, 2009 at 5:50 PM, GeoffH <geoffhartn...@gmail.com> wrote:
>
> > Johan,
>
> > Because of security considerations, I will not be able to post the
> > classes
> > What I will do is to implement a similar mock data and mock data test
> > class with non sensitive information, but representative of the
> > problem
> > Might not be until some time tomorrow
>
> > GeoffH
>
> > On Oct 1, 4:36 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> > > The annotations must always be placed at the class-level of the test.
> > It's
> > > hard to say why it's not working without the code. Is it possible for you
> > to
> > > post the code (test & code under test) on the google group or just cut
> > and
> > > paste it here? In that case I'll try and see if it works in the new
> > version
> > > of PowerMock.
>
> > > /Johan
>
> > > On Thu, Oct 1, 2009 at 4:36 PM, GeoffH <geoffhartn...@gmail.com> wrote:
>
> > > > Johan,
>
> > > > My usage of PowerMock may be different enough to provide a description
> > > > to see if I am using the framework correctly
>
> > > > I have constructed a class call MockData for the purpose of providing
> > > > initialisation data to an existing class (e.g call it ComplexClass)
> > > > that has lots of collaborators - I am trying to decouple the
> > > > collaborators to enable ComplexClass to be tested in isolation
>
> > > > I have also constructed a class MockDataTest which attempts to check
> > > > that the data in MockData will behave as expected when applied to
> > > > ComplexClass. In MockDataTest I have standard JUnit assertions for
> > > > comparing actual and expected when the methods on the mocks are called
>
> > > > In MockData I am attempting to mock MyFinalClassWithStaticMethod using
> > > > PowerMock and Mockito, and it is in this class that I have the
> > > > annotations:
> > > > @RunWith(PowerMockRunner.class)
> > > > @PrepareForTest(MyFinalClassWithStaticMethod.class)
> > > > at the class level
>
> > > > I have no annotations in MockDataTest, although I did try adding them,
> > > > but still got the exceptions described above
> > > > For all other mocks I have just used Mockito, and MockDataTest runs
> > > > and passes tests as expected
>
> > > > Does this look like the correct use of the annotations and PowerMock
> > > > framework
>
> > > > Thanks,
>
> > > > GeoffH
>
> > > > On 1 Oct, 12:36, Johan Haleby <johan.hal...@gmail.com> wrote:
> > > > > I don't know of any other way unfortunately. Hopefully I'll get to
> > > > release a
> > > > > new version next week.
>
> > > > > You could have a look athttp://
> > > > code.google.com/p/powermock/source/browse/tags/powermock-1.2.5...
> > > > > see an example of how you can mock static methods with Mockito and
> > > > > PowerMock 1.2.5. Note though that the syntax will change (slightly)
> > in
> > > > > version 1.3. In 1.3 you now longer pass the class to the
> > "verifyStatic"
> > > > > method.
>
> > > > > /Johan
>
> > > > > On Thu, Oct 1, 2009 at 1:29 PM, GeoffH <geoffhartn...@gmail.com>
Message has been deleted

Johan Haleby

unread,
Oct 4, 2009, 11:12:15 AM10/4/09
to powe...@googlegroups.com
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 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....@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

GeoffH

unread,
Oct 7, 2009, 4:54:34 AM10/7/09
to PowerMock
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 »

Johan Haleby

unread,
Oct 7, 2009, 5:10:16 AM10/7/09
to powe...@googlegroups.com
Hmm that's interesting, I may have an idea what's going on but I need to be able to duplicate this otherwise I cannot know for sure. Is there anyway that you can create an example to that demonstrates this issue as you did before?

/Johan

GeoffH

unread,
Oct 7, 2009, 6:30:42 AM10/7/09
to PowerMock
Johan,

The sample classes (FinalClassMock, FinalClassMockTest, DummyClass and
MyFinalClassWithStaticMethod) are the files that I uploaded previously
I have made no change to these sample classes - the only thing I did
was change
from
Mockito 1.7 with PowerMock 1.2.5
to
PowerMock 1.3

GeoffH

On Oct 7, 10:10 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hmm that's interesting, I may have an idea what's going on but I need to be
> able to duplicate this otherwise I cannot know for sure. Is there anyway
> that you can create an example to that demonstrates this issue as you did
> before?
>
> /Johan
>
> ...
>
> read more »

Johan Haleby

unread,
Oct 7, 2009, 7:12:32 AM10/7/09
to powe...@googlegroups.com
Ah but I got it work on PowerMock 1.3 already (I was using trunk when I investigated your problems). Does this happen in Eclipse or Maven or both? Are you using Maven or are you using it stand-alone (PowerMock 1.3 with Mockito dependencies)? I'll try to create a new project and use the released version of PowerMock 1.3 and see if it works at my place but I don't have to do it until tomorrow evening I think.

/Johan

GeoffH

unread,
Oct 7, 2009, 8:11:22 AM10/7/09
to PowerMock
Yohan

I am running the unit tests in Eclipse (Build id: 20090619-0625)
I downloaded powermock-1.3-with-mockito-dependencies.zip and extracted
to a directory
I removed all the jars associated with Mockito 1.7 and PowerMock 1.2.5
from the build path
I added all the jars from the PowerMock 1.3 directroy to the build
path

No panic if you can't look at it until tomorrow

GeoffH

On Oct 7, 12:12 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> Ah but I got it work on PowerMock 1.3 already (I was using trunk when I
> investigated your problems). Does this happen in Eclipse or Maven or both?
> Are you using Maven or are you using it stand-alone (PowerMock 1.3 with
> Mockito dependencies)? I'll try to create a new project and use the released
> version of PowerMock 1.3 and see if it works at my place but I don't have to
> do it until tomorrow evening I think.
>
> /Johan
>
> ...
>
> read more »

Johan Haleby

unread,
Oct 7, 2009, 8:33:22 AM10/7/09
to powe...@googlegroups.com
Ok that seems right. I'll take a look when I get time.

/Johan

Johan Haleby

unread,
Oct 8, 2009, 1:49:55 AM10/8/09
to powe...@googlegroups.com
Hi,

I tried out your examples again yesterday in a completely new (Eclipse) project using only the jar files from "PowerMock 1.3 with Mockito dependencies" and after I made the changes I described in a previous mail everything worked as expected. So I'm afraid I cannot duplicate the issues you have. Are you sure that you have all correct jars on classpath and that they're actually used?

/Johan

GeoffH

unread,
Oct 8, 2009, 4:20:04 AM10/8/09
to PowerMock
Johan,

I moved the classes that I uploaded into a completely new Eclipse
project like you
Previously, they were created to be representative of classes in
another project
So now they are in a stand alone project

Then I added only the jars form the powermock-1.3-with-mockito-
dependencies.zip download

I have taken some screen shots to show the Eclipse project and JUnit
results and saved these to a Word doc:
PowerMock1.3Test, which I have uploaded
If you change the magnification to 150% or 200% you should be able to
see

This is very strange
1) including the @PrepareForTest causes a problem for me when just
using Mockito to create a mock
- the problem occurs in bot tests creating a 'normal' mock; nothing
to do with PowerMock at this stage

2) commenting out @PrepareForTest allows Mockito to create the
DummyClass mock in the both tests
- but then there is the issue of the Final class with static methods

Can you see anything wrong from the screen shots

Thanks,

GeoffH


On Oct 8, 6:49 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> I tried out your examples again yesterday in a completely new (Eclipse)
> project using only the jar files from "PowerMock 1.3 with Mockito
> dependencies" and after I made the changes I described in a previous mail
> everything worked as expected. So I'm afraid I cannot duplicate the issues
> you have. Are you sure that you have all correct jars on classpath and that
> they're actually used?
>
> /Johan
>
> On Wed, Oct 7, 2009 at 2:33 PM, Johan Haleby <johan.hal...@gmail.com> wrote:
> > Ok that seems right. I'll take a look when I get time.
>
> > /Johan
>
> ...
>
> read more »

Johan Haleby

unread,
Oct 8, 2009, 6:47:59 AM10/8/09
to powe...@googlegroups.com
By looking at the screenshot at page two it seems to me that you're using plain Mockito.mock(..) to create a mock object of a final class (line 48 in class FinalCLassMock) ? Is that so? You need to use PowerMockito.mock(..) in order for this to work. Could you try that?

/Johan

GeoffH

unread,
Oct 8, 2009, 7:41:13 AM10/8/09
to PowerMock
Johan,

In line 48 FinalClassMock, I'm creating a mock of a non final class:
mockDummyClass = mock(DummyClass.class);
- where DummyClass is a non final class:

package my.pkg;
public class DummyClass {
public DummyClass() {
}

public String aMethod() {
return "Real Dummy Class";
}
}

I tried PowerMockito.mock, but it made no difference
I think this is just a normal mock creation and probably can be done
with Mockito alone
Howerver, I could be wrong

Clearly, I have something in these classes that causes PowerMock to
misbehave
Most likely my incorrect use of PowerMock

I'll keep looking, but this is a background task for me

If you have any further thoughts, I'd pe pleased to read them

Thanks for your help so far

GeoffH


On 8 Oct, 11:47, Johan Haleby <johan.hal...@gmail.com> wrote:
> By looking at the screenshot at page two it seems to me that you're using
> plain Mockito.mock(..) to create a mock object of a final class (line 48 in
> class FinalCLassMock) ? Is that so? You need to use PowerMockito.mock(..) in
> order for this to work. Could you try that?
>
> /Johan
>
> ...
>
> read more »

Johan Haleby

unread,
Oct 8, 2009, 11:57:21 AM10/8/09
to powe...@googlegroups.com
I don't know what can be the cause of the problem. I've uploaded an Eclipse project that I created at http://groups.google.com/group/powermock/web/TempMockitoTest.zip which works at my place. Try that and see if you get the same error..The only difference that I can see from your settings is that I'm using Java6. Could that be the issue?

/Johan

GeoffH

unread,
Oct 9, 2009, 3:41:13 AM10/9/09
to PowerMock
Yohan,

Yes, Java6 was the issue
As soon as I changed to Java6 both tests completed with no errors

The project I have just joined has an installed base running on Java5
I'm not aware of any plans to migrate to Java6 at this stage

Are there any plans to make PowerMock run with Java5
- and if so, are there any timescales

Thanks

GeoffH

On Oct 8, 4:57 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> I don't know what can be the cause of the problem. I've uploaded an Eclipse
> project that I created athttp://groups.google.com/group/powermock/web/TempMockitoTest.zipwhich works
> at my place. Try that and see if you get the same error..The only difference
> that I can see from your settings is that I'm using Java6. Could that be the
> issue?
>
> /Johan
>
> ...
>
> read more »

Johan Haleby

unread,
Oct 9, 2009, 4:19:48 AM10/9/09
to powe...@googlegroups.com
Well our aim is that It should work on Java5 as well. If possible try to upgrade to the latest Java5 release (JDK 5.0 Update 21) and see if you have the same problems there. Which version of Java5 are you using right now?

/Johan

GeoffH

unread,
Oct 9, 2009, 5:35:25 AM10/9/09
to PowerMock
Johan,

I managed to get a Java 5 version that is used in production
It is 1.5.11:
>java -version
java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode)
The stand alone project tests now run and pass with 1.5.11
I'm still having problems with the real class and am currently working
on that to see what it is

Thanks

GeoffH

On Oct 9, 9:19 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Well our aim is that It should work on Java5 as well. If possible try to
> upgrade to the latest Java5 release (JDK 5.0 Update 21) and see if you have
> the same problems there. Which version of Java5 are you using right now?
>
> /Johan
>
> ...
>
> read more »

GeoffH

unread,
Oct 27, 2009, 2:03:42 PM10/27/09
to PowerMock
Johan,

I have made significant progress in between other work, but I now have
another problem associated with mocking final classes with static
methods
I may well be missing something, so below is a description of my
environment, and any input is wlecome

I have a class (class1) and its associated test - call it class1test
class1 makes calls to a final class with static methods - call it
staticclass1
staticclass1 makes calls to another final class with static methods -
call this class staticclass2
- this is just the way the project is configured
Both staticclass1 and staticclass2 are made available as classes to
class1 through referenced jars

In class1test I have the following:
a) at the test class declaration level I have
@RunWith(PowerMockRunner.class)
@PrepareForTest(staticclass2.class)
- and there is no test class constructor

b) I have set up expectations in the test for the invoked static
method in staticclass2 with:
PowerMockito.mockStatic(staticclass2.class)
Mockito.when(staticclass2.staticMethod(anyObject(), anyString())).
thenReturn(localTestMethod(DATA1),
localTestMethod(DATA2),
localTestMethod(DATA3),
localTestMethod(DATA4));
- where localTestMethod is a method in class1test and the expectation
is for 4 calls to staticclass2.staticMethod

I then invoke my test which executes a method in class1
The method in class1 calls staticclass1.staticmethod1
staticclass1.staticmethod1 calls staticclass2.staticmethod2

I was anticipating that for each of the calls to
staticclass2.staticmethod2, the set up expectations would be invoked
However, this is not happening

I appreciate that the above can be difficult to follow as it describes
the situation in general terms
If you are able to make any observations or have any input, I would
welcome it

It may be that I have to construct very simple static classes to be
representative of the actual situation in order to go forward
Thanks for any help

GeoffH
> ...
>
> read more »

Johan Haleby

unread,
Oct 27, 2009, 4:11:04 PM10/27/09
to powe...@googlegroups.com
Hi,

Yes it's difficult to know what the problem is when I don't see the code. You expectations and setup looks fine to me as far as I can see. What is the expected output of your expectations and was is the actual outcome? What does the localTestMethod look like? Of course the best thing would be if you provide a code example as you did before.

You could also try to prepare staticclass1 for test (but you shouldn't have to).

/Johan

GeoffH

unread,
Oct 28, 2009, 5:22:13 AM10/28/09
to PowerMock
Johan,

I think the tests are now working
I think I had not got the expectations syntax correct and when I put
debug in the localTestMethod, it caused a problem
So when I did get the expectation syntax correct, the problem in
localTestMethod masked the issue

Thanks for your help

GeoffH

On Oct 27, 8:11 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> Yes it's difficult to know what the problem is when I don't see the code.
> You expectations and setup looks fine to me as far as I can see. What is the
> expected output of your expectations and was is the actual outcome? What
> does the localTestMethod look like? Of course the best thing would be if you
> provide a code example as you did before.
>
> You could also try to prepare staticclass1 for test (but you shouldn't have
> to).
>
> /Johan
>
> ...
>
> read more »

Johan Haleby

unread,
Oct 28, 2009, 5:36:37 AM10/28/09
to powe...@googlegroups.com
Glad to heat that you got it working.

/Johan

GeoffH

unread,
Oct 28, 2009, 12:09:31 PM10/28/09
to PowerMock
Johan,

I managed to get several tests running in Eclipse
Then I attempted to migrate to running under Maven 2 at a command
prompt in Windows XP and ran into errors executing tests

I've uploaded a file PowerMockMaven.zip which includes
a) the source and test files
b) the surefire reports
c) the pom.xml dependency settings for PowerMock / Mockito

I set the dependencies before I found:
http://code.google.com/p/powermock/wiki/Mockito_maven
Because of security contraints we are not able to access external
sites, so the PowerMock / Mockito jars jars have been locally
installed
- using the dependencies that I set

The tests compile and run with no errors in Eclipse
The source and test classes compile under Maven 2
However, when running ' mvn test' I get the errors as shown in the
surefire reports

Have you seen anything like this ?
Any suggestions you have will be welcome and thanks for your help so
far

GeoffH


On Oct 28, 9:36 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Glad to heat that you got it working.
>
> /Johan
>
> ...
>
> read more »

Johan Haleby

unread,
Oct 29, 2009, 3:19:07 AM10/29/09
to powe...@googlegroups.com
Hi,

I'll give it a try when I get some spare time. I don't have Windows though so I have to test it on Linux (ubuntu).

/Johan

GeoffH

unread,
Oct 29, 2009, 12:23:38 PM10/29/09
to PowerMock
Yohan,

I migrated the test classes to a unix host running IBM AIX and after
configuring maven ran
mvn test

This looks like it 'almost' completed correctly

The only problem I can see is that one of the tests threw an
IOException:
Exception in thread "main" java.io.IOException: Resource not found:
"org/joda/time/tz/data/ZoneInfoMap" ClassLoader:
org.powermock.core.classloader.MockClassLoader@1cce1cce
at org.joda.time.tz.ZoneInfoProvider.openResource
(ZoneInfoProvider.java:206)


I've uploded a file PowermockMavenAIX.zip
This contains
a) a log of the 'mvn test' command
b) the surefire reports

The test in question runs in Eclipse on Windows XP without throwing
that exception
- the resource is actually in the joda-time-1.4.jar

Not sure what's going on here, so if you have any suggestions they
will gratefully be accepted

Thanks,

GeoffH


On Oct 29, 7:19 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> I'll give it a try when I get some spare time. I don't have Windows though
> so I have to test it on Linux (ubuntu).
>
> /Johan
>
> ...
>
> read more »

Johan Haleby

unread,
Oct 30, 2009, 3:15:29 AM10/30/09
to powe...@googlegroups.com
Thanks for uploading the files. I will look at it but I'm very busy right now :( Perhaps I can find some time to check it out during the weekend but I cannot make any promises.

/Johan

GeoffH

unread,
Oct 30, 2009, 12:50:20 PM10/30/09
to PowerMock
Johan,

I've uploaded another zip file that shows some differences between
running 'mvn clean test' on Windows
- see PowerMockMavenWindows.zip

This zip contains 3 files, each of which shows the output in a Windows
command prompt when running 'mvn clean test'
The only difference in the command prompt environments is the location
of JAVA_HOME

a) TestWithDefaultIBMJDK - JAVA_HOME set to C:\Program Files (x86)\IBM
\SDP70\jdk
b) TestWithSunJDK1.5.0_21 - JAVA_HOME set to C:\Program Files
(x86)\Java\jdk1.5.0_21
c) TestWithSunJDK1.6.0_16 - JAVA_HOME set to C:\Program Files
(x86)\Java\jdk1.6.0_16

This shows that running 'mvn clean test' on AIX and Windows is
consistent when a Sun JDK is used on Windows
- and results in the joda-time 'Resource not found' problem :

Exception in thread "main" java.io.IOException: Resource not found:
"org/joda/time/tz/data/ZoneInfoMap" ClassLoader:
org.powermock.core.classloader.MockClassLoader@19fcbac1
at org.joda.time.tz.ZoneInfoProvider.openResource
(ZoneInfoProvider.java:206)

If you have time to look at anything, then it would be great if you
could look at this problem

We are using joda-time 1.4 and I got the source from
http://sourceforge.net/projects/joda-time/files/joda-time/
- so that I could attach the source to my Eclipse for debug
However, I don't get the 'Resource not found' problem in Eclipse, so
was not able to get any further insight

Thanks,

GeoffH

On Oct 30, 7:15 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Thanks for uploading the files. I will look at it but I'm very busy right
> now :( Perhaps I can find some time to check it out during the weekend but I
> cannot make any promises.
>
> /Johan
>
Reply all
Reply to author
Forward
0 new messages