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?
8 comments:
I find you present an easy way of testing your service the BlogDAO definition. Will you kindly present this piece of code? I think it would be helpful to shorten the implementation path of your tutorial.
Hmm, this is just an Easymock example and doesn't have a whole lot do with Spring's IoC since you're injecting a mocked DAO yourself.
simple and nice example. thanks
vijay
Very nice one.. Well done
not bad
This is one of the processes which allow you to list and run your websites online or search engines like (Google, Bing, Yahoo, Ask.com, AOL, Baidu, DuckDuckGo etc.) without making any payment to them. But the very important thing in this is it is not so easy to rank in SERP/ Search engine result page as there are several websites have already been listed under these search engine platforms so which needs to work hard to rank better in search engine result page. This process is called as a Search Engine Optimization. data science course syllabus
Mua vé tại đại lý vé máy bay Aivivu, tham khảo
vé máy bay đi Mỹ giá rẻ 2021
khi nào có chuyến bay từ mỹ về việt nam
khi nào mở lại đường bay nhật bản việt nam
ve may bay tư duc ve viet nam
từ canada về việt nam quá cảnh ở đâu
vé máy bay từ hàn quốc về việt nam
The World's Leading Online Casino | Kardangpintar
If you want 온카지노 to make the best online casino experience for your septcasino players, Kardangpintar has you 바카라 사이트 covered. We're an independent and fully licensed operator.
Post a Comment