Junit實現spring的單元測試

來源:互聯網
上載者:User

 spring提供的單元測試是強大的,spring的單元測試很簡單,封裝的很好。我們要用spring的單元測試測試我們寫的add,delete等方法時候需要spring提供的一個額外包spring-mock.jar,我已經傳上來了。你只要熟悉單元測試,編寫一個測試案例,然後把繼承改為org.springframework.test.AbstractTransactionalDataSourceSpringContextTests就可以了,此時編譯器會提示你要實現

  1. public String[] getConfigLocations(){
  2.   
  3.   String[] config = new String[]{"applicationContext.xml",
  4.             "applicationContext-dao.xml",
  5.             "applicationContext-hibernate.xml",
  6.             "applicationContext-service.xml"};
  7.   
  8.   return config;
  9.   
  10. }

  
  看了大家應該明白,就是把你配置好的xml賦值給它,
  
  然後大家就可以通過下面方法:
  
  下面的applicationContext這個變數是你只要繼承了剛才那個抽象類別就可以得到的一個恒量。
  
  FriendService friendService =
(FriendService)applicationContext.getBean("friendService");
  
  得到你的執行個體來進行商務邏輯測試了,是不是很簡單,大家試試吧,它在此時完成以後會把資料庫復原一次,不會影響你的資料庫記錄,非常好。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.