February 3, 2011

Part 8: Testing MyBatis with jUnit

To be certain that the MyBatis configuration and Mapper files you setup in Part 7 are working properly, let's create a jUnit test to make sure.

First, you will need to add the jUnit library to your Eclipse project:

  • Right Click on the ModelingAgency project in Project Explorer
  • Go to Properties -> Java Build Path -> Libraries Tab -> Add Library (button)
  • Select jUnit4 and click Finish

Second, create a new package under your webapp called com.modelingagency.tests.
We will keep our jUnit test classes in here.

Third, add the file MyBatisTest.java to the com.modelingagency.tests package:

package com.modelingagency.tests;

import static org.junit.Assert.*;

import java.io.Reader;
import java.util.ArrayList;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.modelingagency.objects.Booking;
import com.modelingagency.objects.Client;
import com.modelingagency.objects.Model;

public class MyBatisTest {
 
 private static Logger log = LoggerFactory.getLogger(MyBatisTest.class);
 private static SqlSessionFactory sf;
 
  
 @BeforeClass
 public static void setUp() throws Exception {
  log.info("starting up myBatis tests");
  String resource = "mybatis.config.xml";
  Reader reader = Resources.getResourceAsReader(resource);  
  sf = new SqlSessionFactoryBuilder().build(reader,"testing"); //we're using the 

'testing' 
 }
 
 @AfterClass
 public static void tearDown() throws Exception {  
  log.info("closing down myBatis tests");
 }
 
 @Test
 public void getClients(){
    
  SqlSession session = sf.openSession();
  try
  {   
   ArrayList client = (ArrayList)session.selectList

("com.modelingagency.objects.SimpleMappers.allClients");    
   assertNotNull("Client list is null",client);   
  }
  finally
  {
   session.close();
  }
  
 }
  
 @Test
 public void getModels(){
    
  SqlSession session = sf.openSession();
  try
  {   
   ArrayList model = (ArrayList)session.selectList

("com.modelingagency.objects.SimpleMappers.allModels");    
   assertNotNull("Model list is null",model);
  }
  finally
  {
   session.close();
  }
  
 }
 
 @Test
 public void getBookings(){
    
  SqlSession session = sf.openSession();
  try
  {   
   ArrayList bookings = (ArrayList)session.selectList

("com.modelingagency.objects.SimpleMappers.allBookings");    
   assertNotNull("Booking is null",bookings);   
  }
  finally
  {
   session.close();
  }
  
 }
 
 
}

I'm not going into detail into what the MyBatisTest class does, but it should be self explanatory. It loads the MyBatis configuration we have defined, using the 'test' environment, and then runs some test methods against the queries we have defined in the SimpleMappers.xml mapper file.

To run the test, right click on the MyBatisTest.java, and select
Run As -> jUnit Test

If everything went well, you should see the following output in the jUnit window:

Now you're ready to move on.

Part 9: Creating the DAO Layer