qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Spring框架下的單元測試方法

           介紹在Spring的框架下,做單元測試的兩種辦法。
            一、使用spring中對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>
            然后測試類需要繼承自AbstractJUnit4SpringContextTests,這樣就可以在測試類中使用注解簡單的注入需要的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>
            二、手動加載spring的配置文件,并啟動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);
          }
          }
            用這種方式測試,只需要Ctrl+F11就行了

          posted on 2014-08-29 09:43 順其自然EVO 閱讀(274) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 静安区| 来宾市| 宁津县| 睢宁县| 文山县| 张家港市| 邯郸县| 黎川县| 手游| 阜城县| 江西省| 夏邑县| 防城港市| 西藏| 旬邑县| 荔波县| 鹤庆县| 南投市| 海丰县| 华容县| 湘阴县| 神池县| 黔西县| 景德镇市| 连江县| 民乐县| 秭归县| 安平县| 罗平县| 抚松县| 贡嘎县| 静乐县| 遵义市| 涿鹿县| 崇明县| 吉安县| 博兴县| 随州市| 彭州市| 丹巴县| 修文县|