aspose file tools*
The moose likes Testing and the fly likes EasyMock: mocking static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "EasyMock: mocking static methods" Watch "EasyMock: mocking static methods" New topic
Author

EasyMock: mocking static methods

Ronwaldo Cruz
Ranch Hand

Joined: Oct 17, 2006
Posts: 69
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30293
    
150

Ronwaldo ,
See the Class Extension library for mocking statics.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ronwaldo Cruz
Ranch Hand

Joined: Oct 17, 2006
Posts: 69
Thanks a lot!
lavnish lalchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 79
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.

lavnish.blogspot.com
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30293
    
150

Good point. I remember it does mock classes. I don't remember for static methods now that I think about it.
Johan Haleby
Greenhorn

Joined: Mar 09, 2007
Posts: 6
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

Joined: Jul 25, 2009
Posts: 2
I tried to use PowerMock for mocking ServletActionContext.getResponse() call, but it didn't work (((
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30293
    
150

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

Joined: Jul 25, 2009
Posts: 2
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

Joined: Mar 09, 2007
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EasyMock: mocking static methods