氟塑料离心泵www.buybeng.com

jquery教程http://www.software8.co/wzjs/jquery/

Spring单元测试,SQL数据准备

  • 工具类代码

在测试中,一般的数据库数据Mock是不可少的。在前文的基础上,我们写读取脚本把数据写入数据库的工具类。

 

  1. import org.junit.After;  
  2. import org.junit.Before;  
  3. import org.junit.runner.RunWith;  
  4. import org.slf4j.Logger;  
  5. import org.slf4j.LoggerFactory;  
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;  
  8. import org.springframework.test.context.ContextConfiguration;  
  9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  10.   
  11. import javax.sql.DataSource;  
  12. import java.io.BufferedReader;  
  13. import java.io.IOException;  
  14. import java.io.InputStream;  
  15. import java.io.InputStreamReader;  
  16. import java.text.SimpleDateFormat;  
  17. import java.util.Date;  
  18. import java.util.HashMap;  
  19.   
  20. @RunWith(SpringJUnit4ClassRunner.class)  
  21. @ContextConfiguration(locations = {"classpath:testApplicationContext.xml"})  
  22. public class SqlDataPrepareUtil {  
  23.     @Autowired  
  24.     DataSource dataSource;  
  25.     SimpleJdbcTemplate template;  
  26.   
  27.     private final Logger logger = LoggerFactory.getLogger(SqlDataPrepareUtil.class);  
  28.   
  29.     @Before  
  30.     public void setUp() throws IOException {  
  31.   
  32.         template = new SimpleJdbcTemplate(dataSource);  
  33.   
  34.   
  35.         String content = sqlForThisTest();  
  36.         if (content.equals("")) {  
  37.             return;  
  38.         }  
  39.   
  40.         String[] sqlLines = content.split(";");  
  41.         for (int i = 0; i < sqlLines.length; i++) {  
  42.             String sql = sqlLines[i];  
  43.   
  44.             if (0 == sql.trim().length()) {  
  45.                 continue;  
  46.             }  
  47.             template.update(sql);  
  48.             if (logger.isDebugEnabled()) {  
  49.                 logger.debug(sql);  
  50.             }  
  51.         }  
  52.     }  
  53.   
  54.     private String sqlForThisTest() throws IOException {  
  55.         String sqlName = getClass().getSimpleName() + ".sql";  
  56.         InputStream stream = getClass().getResourceAsStream(sqlName);  
  57.         if (stream == null) {  
  58.             return "";  
  59.         }  
  60.         BufferedReader reader = new BufferedReader(  
  61.                 new InputStreamReader(stream, "UTF-8"));  
  62.         StringBuilder buffer = new StringBuilder();  
  63.         try {  
  64.             String line = null;  
  65.             while ((line = reader.readLine()) != null) {  
  66.                 buffer.append(line);  
  67.             }  
  68.         } finally {  
  69.             reader.close();  
  70.         }  
  71.         return buffer.toString();  
  72.     }  
  73.   
  74. }  
//www.heatpress123.net

 

  • 工具类使用

脚本存放的地方和测试类包名路径一致,测试类的写法如下:

 

  1. import org.jboss.resteasy.mock.MockHttpRequest;  
  2. import org.jboss.resteasy.mock.MockHttpResponse;  
  3. import org.junit.Assert;  
  4. import org.junit.Ignore;  
  5. import org.junit.Test;  
  6. import org.junit.runner.RunWith;  
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.stereotype.Component;  
  9. import org.springframework.test.annotation.Rollback;  
  10. import org.springframework.test.context.ContextConfiguration;  
  11. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  12. import org.springframework.transaction.annotation.Transactional;  
  13.   
  14. import javax.ws.rs.core.MediaType;  
  15. import javax.ws.rs.core.Response.Status;  
  16. import java.io.UnsupportedEncodingException;  
  17. import java.net.URISyntaxException;  
  18. import java.util.ArrayList;  
  19. import java.util.List;  
  20.   
  21. import static com.natpryce.makeiteasy.MakeItEasy.*;  
  22.   
  23. @Component  
  24. public class LineSyncTest extends SqlDataPrepareUtil{  
  25.   
  26.   
  27.     @Test  
  28.     @Rollback(true)  
  29.     public void should_return_XXX_when_given_xx(){  
  30.   
  31.     }  
  32.   
  33. }  

posted on 2012-09-25 14:53 你爸是李刚 阅读(1093) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2012年9月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

常用链接

留言簿

随笔档案

文章档案

技术网站

行业网站

搜索

最新评论

阅读排行榜

评论排行榜

站长网 氟塑料离心泵 注塑机 液晶广告机