qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問(wèn) http://qaseven.github.io/

          Spring框架下的單元測(cè)試方法

           介紹在Spring的框架下,做單元測(cè)試的兩種辦法。
            一、使用spring中對(duì)Junit框架的整合功能
            除了junit4和spring的jar包,還需要spring-test.jar。引入如下依賴:
          <span style="font-size:18px;"><span style="white-space:pre">        </span><dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>3.1.1.RELEASE</version>
          </dependency></span>
            然后測(cè)試類需要繼承自AbstractJUnit4SpringContextTests,這樣就可以在測(cè)試類中使用注解簡(jiǎn)單的注入需要的bean了。
          <span style="font-size:18px;"><span style="color:#ff0000;">@RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration({"classpath:applicationContext.xml"})</span>
          public class ReadDaoImplTest extends<span style="color:#ff0000;"> AbstractJUnit4SpringContextTests</span>{
          @Resource ReadDao readDao;
          @Test
          public void getListTest(){
          List<Client> clientList = readDao.getList("client.test", null);
          for(Client c:clientList){
          System.out.println(c.getVersionNum());
          }
          }
          }
          </span>
            二、手動(dòng)加載spring的配置文件,并啟動(dòng)spring容器
          public class ReadDaoImplTest {
          public  static void main(String[] args){
          ClassPathXmlApplicationContext context = <span style="color:#ff0000;">new ClassPathXmlApplicationContext("applicationContext.xml");</span>
          context.start();
          ReadDao fqaService = (ReadDao) context.getBean("readDao");
          System.out.println(fqaService);
          }
          }
            用這種方式測(cè)試,只需要Ctrl+F11就行了

          posted on 2014-08-29 09:43 順其自然EVO 閱讀(279) 評(píng)論(0)  編輯  收藏 所屬分類: 測(cè)試學(xué)習(xí)專欄

          <2014年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 南城县| 芮城县| 阳春市| 清苑县| 松潘县| 台北市| 宜兰市| 惠州市| 临漳县| 余干县| 冕宁县| 社会| 大余县| 阿合奇县| 霞浦县| 武冈市| 通江县| 榆树市| 黑龙江省| 驻马店市| 郸城县| 怀化市| 蕉岭县| 云南省| 仙桃市| 宜宾市| 新安县| 凯里市| 柳州市| 湛江市| 泸水县| 塔河县| 平顶山市| 略阳县| 南通市| 上虞市| 诸城市| 新蔡县| 赣州市| 山东| 眉山市|