wiki:demo_02

单元测试应用示例——测试DAO层对象

cn.com.pcgames.gamehall2.repository.impl.GameDAOImpl是GameDAO实现类,完成Game对象在数据库中增删改查的功能.
我们在DAO层使用了Spring JdbcTemplate封装了DAO操作,我们使用Spring测试框架辅助编写单写单元测试,以支持数据库自动回滚,以防止对数据库数据的破坏,可以简化测试。
我们让测试类去继承AbstractTransactionalJUnit4SpringContextTests

    /**
 * GameDAO测试
 * @author ChenYang
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})  
public class GameDAOImplTest extends AbstractTransactionalJUnit4SpringContextTests{

......

}

使用Autowired注解,注入GameDAO

  @Autowired
   private GameDAO gameDAO;

测试代码示例如下:

  @Autowired
    @Autowired
    private GameDAO gameDAO;
    
    private Game newGame(){
        Game game = new Game();
        Date now = null;
        try {
            now = DateUtils.parseDate("2011-11-16 23:23:23", new String[]{"yyyy-MM-dd HH:mm:ss"});
        } catch (ParseException ex) {
            Logger.getLogger(GameDAOImplTest.class.getName()).log(Level.SEVERE, null, ex);
        }
        game.setCreateAt(now);
        game.setUpdateAt(now);
        game.setName("三十六计");
        return game;
    }
    
    @Test
    public void testAddGame(){
        long gameId = gameDAO.createGame(newGame());
        Assert.assertTrue(gameId > 0);
    }
    
    @Test
    public void testFindGame(){
        long gameId = gameDAO.createGame(newGame());
        Game game = gameDAO.findGame(gameId);
        Assert.assertNotNull(game);
    }
    
    @Test
    public void testUpdateGame(){
        long gameId = gameDAO.createGame(newGame());
        Game game = gameDAO.findGame(gameId);
        game.setName("三十七计");
        int result = gameDAO.updateGame(game, gameId);
        Assert.assertEquals(1, result);
    }
    
    @Test
    public void testDelGame(){
        long gameId = gameDAO.createGame(newGame());
       int result = gameDAO.deleteGame(gameId);
       Assert.assertEquals(1, result);
    }
    
    @Test
    public void testListGameInPagination(){
        
        int count = gameDAO.countGames();
        int size = count >= 20 ? 20 : count;
        
        List<Game> list = gameDAO.listGamesInPagination(1, 20, "gameId");
        Assert.assertEquals(size, list.size());
        
    }
    
        
        @Test
    public void testEncode(){
                
           @SuppressWarnings("unchecked") 
           AbstractRepository<Game> repository = (AbstractRepository<Game>)gameDAO;
       String value = repository.encode(newGame());
       
       String expected = "gameId:0,name:{4}三十六计,createAt:1321457003000,updateAt:1321457003000,rate:0.0,scale:0.0,pageWidth:0,pageHeight:0,fcm:0";
       Assert.assertEquals(expected, value);
    }
    
    @Test
    public void testDecode(){
       @SuppressWarnings("unchecked")
       AbstractRepository<Game> repository = (AbstractRepository<Game>)gameDAO;
       String value = "gameId:0,name:{4}三十六计,createAt:1321457003000,updateAt:1321457003000,rate:0.0,scale:0.0,pageWidth:0,pageHeight:0,fcm:0";
       Game game = repository.decode(value);
       
       Assert.assertEquals("三十六计", game.getName());
       
       DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Assert.assertEquals("2011-11-16 23:23:23", f.format(game.getCreateAt()));
    }
    
    @Test
    public void countGames(){
        
        gameDAO.createGame(newGame());
        gameDAO.createGame(newGame());
        gameDAO.createGame(newGame());
        
        int count = gameDAO.countGames();
        
        Assert.assertTrue(count >= 3);
    }