Service level testing in spring mvc using easymock

Service Interface:

public List<UserAccount> getUserAccounts(); public List<UserAccount> getUserAccounts(ResultsetOptions resultsetOptions, List<SortOption> sortOptions); 

Service implementation:

 public List<UserAccount> getUserAccounts() { return getUserAccounts(null, null); } public List<UserAccount> getUserAccounts(ResultsetOptions resultsetOptions, List<SortOption> sortOptions) { return getUserAccountDAO().getUserAccounts(resultsetOptions, sortOptions); } 

How can I test this with easymock or any other viable testing methodology? Sample code will be appreciated. For a light layout of passing objects as parameters is very confusing. Does someone clearly explain what is best for testing service levels? Will the test service interface be considered a unit test or an integration test?

+7
source share
1 answer

Here you go, assuming you're using JUnit 4 with annotations:

 import static org.easymock.EasyMock.createStrictMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; public class UserAccountServiceTest private UserAccountServiceImpl service; private UserAccountDAO mockDao; /** * setUp overrides the default, We will use it to instantiate our required * objects so that we get a clean copy for each test. */ @Before public void setUp() { service = new UserAccountServiceImpl(); mockDao = createStrictMock(UserAccountDAO.class); service.setUserAccountDAO(mockDao); } /** * This method will test the "rosy" scenario of passing a valid * arguments and retrieveing the useraccounts. */ @Test public void testGetUserAccounts() { // fill in the values that you may want to return as results List<UserAccount> results = new User(); /* You may decide to pass the real objects for ResultsetOptions & SortOptions */ expect(mockDao.getUserAccounts(null, null) .andReturn(results); replay(mockDao); assertNotNull(service.getUserAccounts(null, null)); verify(mockDao); } } 

You can also find this article , especially if you are using JUnit 3.

See this for quick help on JUnit 4.

Hope this helps.

+4
source

All Articles