Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking a concrete class using EasyMock

Tags:

java

easymock

Is it possible to mock a concrete class using EaskMock? If so, how do I do it?

like image 923
Samuel Carrijo Avatar asked Aug 08 '09 16:08

Samuel Carrijo


People also ask

Can you mock a concrete class?

5 Answers. Show activity on this post. In theory there is absolutely no problem mocking a concrete class; we are testing against a logical interface (rather than a keyword interface ), and it does not matter whether that logical interface is provided by a class or interface . In practice .

How do you mock a void in EasyMock?

If we just want to mock void method and don't want to perform any logic, we can simply use expectLastCall(). andVoid() right after calling void method on mocked object. You can checkout complete project and more EasyMock examples from our GitHub Repository.


4 Answers

See the EasyMock Class Extension documentation and download it from the SourceForge project. You can't mock final methods though.

EDIT: This is now part of EasyMock for v3 and above, as noted in comments.

like image 114
Jon Skeet Avatar answered Sep 25 '22 13:09

Jon Skeet


Powermock extends EasyMock and allows you to mock concrete types, even final and static methods.

PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.

like image 28
Rich Seller Avatar answered Sep 21 '22 13:09

Rich Seller


Yes: http://easymock.org/EasyMock2_4_ClassExtension_Documentation.html

like image 38
jqno Avatar answered Sep 25 '22 13:09

jqno


EasyMock class extensions have been integrated into EasyMock as of v3.0; see the release notes.

like image 45
Mike Partridge Avatar answered Sep 22 '22 13:09

Mike Partridge