qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 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>
            二、手動(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);
          }
          }
            用這種方式測試,只需要Ctrl+F11就行了

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

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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 霍山县| 灵寿县| 襄汾县| 康保县| 荆门市| 凭祥市| 安丘市| 鄂温| 苍南县| 元氏县| 澄城县| 饶河县| 景谷| 满洲里市| 郯城县| 九龙坡区| 南木林县| 时尚| 保亭| 和田市| 淳化县| 宜阳县| 铅山县| 海城市| 图们市| 扬中市| 萝北县| 刚察县| 桂平市| 辽宁省| 西城区| 马山县| 安福县| 邢台市| 从化市| 房产| 含山县| 西昌市| 南投县| 宁波市| 枝江市|