Changes between Version 2 and Version 3 of demo_02


Ignore:
Timestamp:
04/05/2012 02:58:57 PM (14 years ago)
Author:
chenyang
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • demo_02

    v2 v3  
    22== 单元测试应用示例——测试DAO层对象 == 
    33cn.com.pcgames.gamehall2.repository.impl.GameDAOImpl是GameDAO实现类,完成Game对象在数据库中增删改查的功能.[[BR]] 
    4 我们在DAO层使用了Spring JdbcTemplate封装了DAO操作,我们使用Spring测试框架辅助编写单写单元测试,可以简化测化,我们让测试类去继承AbstractTransactionalJUnit4SpringContextTests[[BR]] 
     4我们在DAO层使用了Spring JdbcTemplate封装了DAO操作,我们使用Spring测试框架辅助编写单写单元测试,以支持数据库自动回滚,以防止对数据库数据的破坏,可以简化测化,我们让测试类去继承AbstractTransactionalJUnit4SpringContextTests[[BR]] 
    55{{{ #!java 
    66    /** 
     
    1616} 
    1717}}} 
     18 
     19使用Autowired注解,注入GameDAO  
     20{{{ #!java 
     21  @Autowired 
     22   private GameDAO gameDAO; 
     23}}} 
     24 
     25测试代码示例如下: 
     26{{{ #!java 
     27  @Autowired 
     28    @Autowired 
     29    private GameDAO gameDAO; 
     30     
     31    private Game newGame(){ 
     32        Game game = new Game(); 
     33        Date now = null; 
     34        try { 
     35            now = DateUtils.parseDate("2011-11-16 23:23:23", new String[]{"yyyy-MM-dd HH:mm:ss"}); 
     36        } catch (ParseException ex) { 
     37            Logger.getLogger(GameDAOImplTest.class.getName()).log(Level.SEVERE, null, ex); 
     38        } 
     39        game.setCreateAt(now); 
     40        game.setUpdateAt(now); 
     41        game.setName("三十六计"); 
     42        return game; 
     43    } 
     44     
     45    @Test 
     46    public void testAddGame(){ 
     47        long gameId = gameDAO.createGame(newGame()); 
     48        Assert.assertTrue(gameId > 0); 
     49    } 
     50     
     51    @Test 
     52    public void testFindGame(){ 
     53        long gameId = gameDAO.createGame(newGame()); 
     54        Game game = gameDAO.findGame(gameId); 
     55        Assert.assertNotNull(game); 
     56    } 
     57     
     58    @Test 
     59    public void testUpdateGame(){ 
     60        long gameId = gameDAO.createGame(newGame()); 
     61        Game game = gameDAO.findGame(gameId); 
     62        game.setName("三十七计"); 
     63        int result = gameDAO.updateGame(game, gameId); 
     64        Assert.assertEquals(1, result); 
     65    } 
     66     
     67    @Test 
     68    public void testDelGame(){ 
     69        long gameId = gameDAO.createGame(newGame()); 
     70       int result = gameDAO.deleteGame(gameId); 
     71       Assert.assertEquals(1, result); 
     72    } 
     73     
     74    @Test 
     75    public void testListGameInPagination(){ 
     76         
     77        int count = gameDAO.countGames(); 
     78        int size = count >= 20 ? 20 : count; 
     79         
     80        List<Game> list = gameDAO.listGamesInPagination(1, 20, "gameId"); 
     81        Assert.assertEquals(size, list.size()); 
     82         
     83    } 
     84     
     85         
     86        @Test 
     87    public void testEncode(){ 
     88                 
     89           @SuppressWarnings("unchecked")  
     90           AbstractRepository<Game> repository = (AbstractRepository<Game>)gameDAO; 
     91       String value = repository.encode(newGame()); 
     92        
     93       String expected = "gameId:0,name:{4}三十六计,createAt:1321457003000,updateAt:1321457003000,rate:0.0,scale:0.0,pageWidth:0,pageHeight:0,fcm:0"; 
     94       Assert.assertEquals(expected, value); 
     95    } 
     96     
     97    @Test 
     98    public void testDecode(){ 
     99       @SuppressWarnings("unchecked") 
     100       AbstractRepository<Game> repository = (AbstractRepository<Game>)gameDAO; 
     101       String value = "gameId:0,name:{4}三十六计,createAt:1321457003000,updateAt:1321457003000,rate:0.0,scale:0.0,pageWidth:0,pageHeight:0,fcm:0"; 
     102       Game game = repository.decode(value); 
     103        
     104       Assert.assertEquals("三十六计", game.getName()); 
     105        
     106       DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     107       Assert.assertEquals("2011-11-16 23:23:23", f.format(game.getCreateAt())); 
     108    } 
     109     
     110    @Test 
     111    public void countGames(){ 
     112         
     113        gameDAO.createGame(newGame()); 
     114        gameDAO.createGame(newGame()); 
     115        gameDAO.createGame(newGame()); 
     116         
     117        int count = gameDAO.countGames(); 
     118         
     119        Assert.assertTrue(count >= 3); 
     120    } 
     121}}}