Testing Spring applications with jUnit 4 and Easy Mock

Posted on August 24, 2008

2


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

  1. package com.teja.tests;
  2. import java.util.List;
  3. /**
  4. *
  5. * @author Teja Kantamneni
  6. */
  7. public interface BlogSearchService {
  8. public List<Blog> searchBlogByKeyWord(String keyword);
  9. }

BlogSearchServiceImpl.java

  1. package com.teja.tests;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. /**
  5. *
  6. * @author Teja Kantamneni
  7. */
  8. public class BlogSearchServiceImpl implements BlogSearchService {
  9. @Autowired
  10. BlogDAO blogDAO;
  11. public List<Blog> searchBlogByKeyWord(String keyword){
  12. return blogDAO.searchByKeyword(keyword);
  13. }
  14. public void setBlogDAO(BlogDAO blogDAO) {
  15. this.blogDAO = blogDAO;
  16. }
  17. }

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.

  1. package com.teja.tests;
  2. import java.util.Collections;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import static org.easymock.EasyMock.*;
  10. /**
  11. *
  12. * @author Teja Kantamneni
  13. */
  14. @RunWith(SpringJUnit4ClassRunner.class)
  15. @ContextConfiguration(locations = {“classpath:beans.xml”})
  16. public class BlogSearchServiceTest {
  17. @Autowired
  18. BlogSearchServiceImpl blogSearchService;
  19. BlogDAO blogDAO;
  20. @Before
  21. public void setUp(){
  22. blogSearchService = new BlogSearchServiceImpl();
  23. blogDAO = createMock(BlogDAO.class);
  24. blogSearchService.setBlogDAO(blogDAO);
  25. }
  26. @Test
  27. public void testSearchBlogByKeyword(){
  28. String keyword = “spring”;
  29. expect(blogDAO.searchByKeyword(keyword)).andReturn(Collections.EMPTY_LIST);
  30. replay(blogDAO);
  31. blogSearchService.searchBlogByKeyWord(keyword);
  32. verify(blogDAO);
  33. }
  34. }

It’s so simple and sweet testing spring classes with jUnit and easymock. Isn’t it?

Advertisements