7

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 using easymock or any other viable testing methodology? sample code will be appreciated. For the easy mock passing objects as parameters very confusing. Some one clearly explain whats the best approach to test the service layer? testing service interface will be considered as unit test or integration test?

Nilesh
  • 4,137
  • 6
  • 39
  • 53
kneethan
  • 423
  • 1
  • 8
  • 22
  • 1
    Asking question on Friday evening not a good idea since no one looks this in weekend and in Monday they will be busy with new questions. I'm sure some testing gurus surely know the answer and explanation for this and will help me out. – kneethan Mar 21 '11 at 14:36

1 Answers1

4

Here you go, assuming you are 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 might also find this article useful especially if you are using JUnit 3.

Refer this for a quick help on JUnit 4.

Hope that helps.

Nilesh
  • 4,137
  • 6
  • 39
  • 53
  • Thanks for your response, so whenever we encounter some object parameters which are not relevant to unit test then we make them as null in the expect call ? – kneethan Mar 22 '11 at 19:51
  • Is it really make sense to write unit test case for these two methods? for me its nothing but delegating to DAO layer, still we need to test? – kneethan Mar 22 '11 at 19:52
  • For a simple getter it may not - however - if you have conditions and if/else path in the service method then it would. One other thing the whole purpose of unit testing is to unit test a single unit (Service in this case) not Service and DAO (That is called integration testing).As to passing null, I chose the simplest test. But as put in the comment, you should create those objects (ResultSetOptions, SortOptions) too. If they are not ready (i.e. only interfaces and no implementation) then you may have to use mock implementation for them too. – Nilesh Mar 23 '11 at 02:47
  • @kneethan - it would also be helpful if you upvote - so that right ansewr/content moves up. Thanks. – Nilesh Mar 23 '11 at 02:49
  • You could also choose to not mock the DAO but the layer underneath the DAO, to make the tests more flexible: http://www.baeldung.com/2011/10/02/testing-the-service-layer/ – Eugen Oct 20 '11 at 08:41