Ocassionally, some of your methods in your classes need to call some other public methods in the same class to get the job done. But when writing unit tests for the calling methods, you want to keep your tests isolated from the calls to other public methods. I encountered such a scenario recently and found that Easymock can help you by mocking some of the methods while keeping the rest of the methods intact.
Let us see an example,
The BlogSearchService has two public methods, first method filterBlogsByAuthor takes an author name and a list of blogs and returns all the blogs written by that particular order author.
The other business method searchBlogs will take a search paramter and the author, it will get all the blogs by the search parameter using a custom logic and do some other house keeping tasks like sorting them by published date and number of comments and then filter the methods by author.
When writing the test cases for the second method, I don't want to test the other public method filterByAuthor as
(i)it has already been tested by other test cases,
(ii)I want to keep my test cases more unit.
So in my test case where I am testing my searchBlogs, I am creating the mock service class which has mock methods only for filterBlogsByAuthor, but the actual mehtod(searchBlogs) which is about to be tested is not.
As usual you can do expectations on the mock class, replay and verify as if it is some other external resource being mocked.
To create a partial mock class of the service, the extra parameter you need to pass is the name of the method to be mocked and the parameter types it accepts (coz, it need to know which version of the method you want to mock if you have overloaded methods)
Here is our test class:
7 comments:
Sorry, but I cannot seem to find such a createMock method in EasyMock 2.4.
Which version were you using to write that test?
check the EasyMock class, that method is a static
I found it!
The method you use is not in the "core" EasyMock, but in the EasyMockClassExtension!
Thanks for your article! :-)
Hey Teja,
Great article! This was what I was looking for. I hope all is well with you (including married life ;). Keep in touch!
Michael
Would you ming giving us some examples of mocking static methods ?
I have been struggled with static methods and singletons for testability :(
You might want to consider using PowerMock (http://code.google.com/p/powermock/) for mocking private and static methods.
Very helpful blog.
Thank you :)
Post a Comment