a peek into my MIND

February 4, 2010

Unit testing iBATIS SqlMapClient with EasyMock

Filed under: Java, Testing — Bharat Kondeti @ 3:44 pm

Integrating iBATIS to an application using spring framework is pretty straight forward. Testing a DAO code with live database will make it an integration test.

In general I write two kinds of tests for any DAO code

  • Unit Test – Using EasyMock to mock a DataSource, SqlMapSession and SqlMapClient.
  • Integration test – Using DBUnit to create and delete the db data.

Following is an example DAO code. We are using iBATIS with spring. Code can be much more complex with multiple calls to database and can have some logic inside it.

import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class SomeDAODBImpl extends SqlMapClientDaoSupport {
	
  public List<Customer> lookupCustomers(String zip) {
    List<Customer> customerList = null;
    try {
      SqlMapClientTemplate template = getSqlMapClientTemplate();
      customerList = template.queryForList("Some-Ibatis-Statement-Name", zip);
    } catch (Exception ex) {
       //TODO: Do something with the caught exception
    }
    return customerList;
  }
}

To unit test this code one have to create a mock datasource, session and client. Following is an example unit test code.

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapSession;

public class SomeDAODBImplTest {

  // Creating IMocksControl for datasource, session and client
  IMocksControl controlClient;
  IMocksControl controlSession;
  IMocksControl controlDataSource;
  SqlMapSession session;
  SqlMapClient client;
  SomeDAODBImpl someDAODBImpl;

  @Before
  public void setUp() throws Exception {
    // Initializing the mocks
    controlClient = EasyMock.createNiceControl();   
    client = controlClient.createMock(SqlMapClient.class);
    
    controlSession = EasyMock.createNiceControl();   
    session = controlSession.createMock(SqlMapSession.class);   
    
    controlDataSource = EasyMock.createNiceControl();   
    DataSource dataSource = controlDataSource.createMock(DataSource.class);
    
    // Mocking openSessions() and getDataSource() methods
    client.openSession();
    EasyMock.expectLastCall().andReturn(session).anyTimes();
    
    client.getDataSource();
    EasyMock.expectLastCall().andReturn(dataSource).anyTimes();
    
    someDAODBImpl = new SomeDAODBImpl();
    someDAODBImpl.setSqlMapClient(client);
  }

  @Test
  public void testLookupCustomersForException() throws Exception {
    session.queryForList(EasyMock.isA(String.class), EasyMock.anyObject());
    EasyMock.expectLastCall().andThrow(new SQLException("Some thing bad happened at database"));
    
    controlClient.replay();
    controlSession.replay();
    controlDataSource.replay();
    
    someDAODBImpl.lookupCustomers("12345");
    
    controlClient.verify();
    controlSession.verify();
    controlDataSource.verify();
  }

  @Test
  public void testLookupCustomersForCustomers() throws Exception {
    List<Customer> customerList = new ArrayList<Customer>();
    customerList.add(Fixture.createCustomer1());
    customerList.add(Fixture.createCustomer2());
    
    session.queryForList(EasyMock.isA(String.class), EasyMock.anyObject());
    EasyMock.expectLastCall().andReturn(customerList);
    
    controlClient.replay();
    controlSession.replay();
    controlDataSource.replay();
    
    List<Customer> returnedCustomerList = someDAODBImpl.lookupCustomers("12345");
    
    controlClient.verify();
    controlSession.verify();
    controlDataSource.verify();
    
    Assert.assertNotNull(returnedCustomerList);
    Assert.assertEquals(2, returnedCustomerList.size());
  }
}
Advertisements

1 Comment »

  1. someDAODBImpl.setSqlMapClient(client); // NOTE: this will fail!!

    as the methods of SqlMapClientDaoSupport are `final`.

    Comment by Vik — September 20, 2013 @ 5:52 pm


RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: