Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EasyMock: mocking static methods

 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
 
lavnish lalchandani
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Johan Haleby
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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.
 
Dmytro Dzyuma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic