氟塑料離心泵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 你爸是李剛 閱讀(1108) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2012年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          技術網站

          行業網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          站長網 氟塑料離心泵 注塑機 液晶廣告機
          主站蜘蛛池模板: 南和县| 增城市| 洪泽县| 万州区| 临洮县| 三亚市| 突泉县| 祁连县| 九江县| 昭觉县| 临潭县| 松阳县| 垦利县| 洪雅县| 房产| 佳木斯市| 资兴市| 化州市| 绥江县| 房产| 伊宁县| 运城市| 容城县| 六枝特区| 惠州市| 舟山市| 吉木萨尔县| 蕉岭县| 龙口市| 博乐市| 来宾市| 兴安盟| 东方市| 彩票| 双流县| 阳高县| 辰溪县| 永靖县| 临安市| 婺源县| 昭平县|