• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EasyMock: mocking static methods

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In Easy mock, you mock methods through:

someClass = createMock(SomeClass.class);

classUnderTest.setSomeClass(someClass ):

expect(someClass.someMethod()).andReturn(1);

Just wondering how to mock a static method call. Suppose in ClassUnderTest there is a call to

SomeClass2.someStaticMethod();

Thanks.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ronwaldo ,
See the Class Extension library for mocking statics.
 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.easymock.org/EasyMock2_4_ClassExtension_Documentation.html#Advanced
Went through above link
It says that it cant test "private" and "final" methods , doesn't mention static methods

I am sorry if i missed something on link.
Just wanted to cross-check.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point. I remember it does mock classes. I don't remember for static methods now that I think about it.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use PowerMock to mock static methods. It just extends EasyMock just like EasyMock class extension does but adds some more functionality so that you can mock static methods, private methods, suppress static initializers, access private state, mock construction of new objects and many other things.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to use PowerMock for mocking ServletActionContext.getResponse() call, but it didn't work (((
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dmytro,
Welcome to JavaRanch!

Dmytro Dzyuma wrote:I tried to use PowerMock for mocking ServletActionContext.getResponse() call, but it didn't work (((


What does "it didn't work" mean? Did you get an error message? See TellTheDetails for more details.
 
Dmytro Dzyuma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to mock this call using "Mocking system classes" approach (http://code.google.com/p/powermock/wiki/MockSystem), but always got NullPointerException at line of expectation:

 
Johan Haleby
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use the "Mocking static methods" approach (http://code.google.com/p/powermock/wiki/MockStatic) since ServletActionContext is not a Java system/bootstrap class. Have a look at the example, I think your case should be about the same.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic