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:
Showing posts with label testing. Show all posts
Showing posts with label testing. Show all posts
Thursday, September 11, 2008
Saturday, August 23, 2008
Testing Spring applications with jUnit 4 and Easy Mock
Unit tests help us make sure that the unit chunks of code being developed are correct. Unit test helps find bugs in the early stages of software development.
Java unit testing frameworks like jUnit, TestNG with some other pretty frameworks like easy mock and easy mock class extensions help us test individual classes with mocking out the external dependent classes/resources and make the tests more "unit".
Spring out of the box supports unit testing with jUnit and TestNG. Spring provides seperate set of classes help you unit test your spring classes.
Spring 2.5 in its org.springframework.test.context package has a set of classes to have better unit testing using jUnit4.
I am about to show an example of testing a service called BlogSearchService. The service simply calls a method in the blogDAO which searches the blogs based on the keyword and return a list of blogs. So here my service class is simple, it calls the DAO. So in my test I just have to make sure that it makes a DAO call and the result is then returned to the client.
Lets see the code for BlogSearchService and BlogSearchServiceImpl,
BlogSearchService.java
BlogSearchServiceImpl.java
Here is the test case for BlogSearchService,
In the test case, the ContextConfiguration helps in specifying the config file to load. In the @Before (setup method for the test case) I am creating the instance of BlogSearchServiceImpl (but instead you can autowire it). Then I am creating the mock object of blogDAO and setting it to the BlogSearchService. In the actual test method, I expect that the searchBlogByKeyWord on the blogDAO and I am expecting it retuning no posts. You can do any asserts here depending on the actual business.
It's so simple and sweet testing spring classes with jUnit and easymock. Isn't it?
Java unit testing frameworks like jUnit, TestNG with some other pretty frameworks like easy mock and easy mock class extensions help us test individual classes with mocking out the external dependent classes/resources and make the tests more "unit".
Spring out of the box supports unit testing with jUnit and TestNG. Spring provides seperate set of classes help you unit test your spring classes.
Spring 2.5 in its org.springframework.test.context package has a set of classes to have better unit testing using jUnit4.
I am about to show an example of testing a service called BlogSearchService. The service simply calls a method in the blogDAO which searches the blogs based on the keyword and return a list of blogs. So here my service class is simple, it calls the DAO. So in my test I just have to make sure that it makes a DAO call and the result is then returned to the client.
Lets see the code for BlogSearchService and BlogSearchServiceImpl,
BlogSearchService.java
- package com.teja.tests;
- import java.util.List;
- /**
- *
- * @author Teja Kantamneni
- */
- public interface BlogSearchService {
- }
BlogSearchServiceImpl.java
- package com.teja.tests;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- /**
- *
- * @author Teja Kantamneni
- */
- public class BlogSearchServiceImpl implements BlogSearchService {
- @Autowired
- BlogDAO blogDAO;
- return blogDAO.searchByKeyword(keyword);
- }
- public void setBlogDAO(BlogDAO blogDAO) {
- this.blogDAO = blogDAO;
- }
- }
Here is the test case for BlogSearchService,
In the test case, the ContextConfiguration helps in specifying the config file to load. In the @Before (setup method for the test case) I am creating the instance of BlogSearchServiceImpl (but instead you can autowire it). Then I am creating the mock object of blogDAO and setting it to the BlogSearchService. In the actual test method, I expect that the searchBlogByKeyWord on the blogDAO and I am expecting it retuning no posts. You can do any asserts here depending on the actual business.
SpringJUnit4ClassRunner is a custom extension of JUnit4ClassRunner which provides functionality of the Spring TestContext Framework to standard JUnit 4.4+ tests by means of the TestContextManager and associated support classes and annotations.
- package com.teja.tests;
- import java.util.Collections;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import static org.easymock.EasyMock.*;
- /**
- *
- * @author Teja Kantamneni
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath:beans.xml"})
- public class BlogSearchServiceTest {
- @Autowired
- BlogSearchServiceImpl blogSearchService;
- BlogDAO blogDAO;
- @Before
- public void setUp(){
- blogSearchService = new BlogSearchServiceImpl();
- blogDAO = createMock(BlogDAO.class);
- blogSearchService.setBlogDAO(blogDAO);
- }
- @Test
- public void testSearchBlogByKeyword(){
- String keyword = "spring";
- replay(blogDAO);
- blogSearchService.searchBlogByKeyWord(keyword);
- verify(blogDAO);
- }
- }
It's so simple and sweet testing spring classes with jUnit and easymock. Isn't it?
Subscribe to:
Posts (Atom)