當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring应用的单元测试
生活随笔
收集整理的這篇文章主要介紹了
Spring应用的单元测试
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?Spring應用的單元測試
?????? 單元測試現在越來越被廣泛重視起來,而Spring更是將時下比較流行的Junit開元測試框架進行整合下面我簡單的介紹一下在Sping中該如何對代碼進行單元測試(本節會認為讀者已經具備了Junit基礎方面的知識)。按照Spring的推薦,在單元測試時不應該依賴于Spring容器,也就是說不應該在單元測試是啟動ApplicationContext并從中獲取Bean,相反應該通過模擬對象完成單元測試。而Spring就提供了這樣一個類供大家繼承。下面來看看示例代碼: ?????? 1)自動裝配的測試用例 代碼清單1 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.tony.web.dao.FooDao; ? @Service public class FooService { ??? @Autowired ??? private FooDao dao; ??? public String save(String name){ ?????? if(name == null || "".equals(name)) ?????????? throw new RuntimeException("Name is null"); ?????? return dao.save(name); ??? } } import org.springframework.stereotype.Repository; @Repository public class FooDao { ??? public String save(String name){ ?????? return "success"; ??? } } import org.springframework.test. AbstractDependencyInjectionSpringContextTests; import com.tony.web.service.FooService; public class MyTest extends AbstractDependencyInjectionSpringContextTests{ ??? protected FooService fooService; ??? //set方法 ??? public void setFooService(FooService fooService) { ?????? this.fooService = fooService; ??? } ??? //指定Spring配置文件的位置 ??? protected String[] getConfigLocations(){ ?????? return new String[]{"spring-config-beans.xml"}; ??? } ??? //測試方法 ??? public void testSave(){ ?????? String str = this.fooService.save("Tony"); ?????? System.out.print(str); ?????? assertEquals("success", str); ??? } } <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://...> ??? <context:component-scan base-package="com.tony"/> </beans> 代碼清單1中定義了FooService.java和FooDao.java兩個Bean已經使用 @Autowired進行了裝配,我們的單元測試類MyTest繼承了 AbstractDependencyInjectionSpringContextTests類,配置好fooService的set方法并且指定Spring配置文件的位置后,當測試用例運行時我們需要的fooService會自動注入進來,我們只要在testSave方法中直接使用就可以了,還有兩外一種寫法 代碼清單2 public class MyTest extends AbstractDependencyInjectionSpringContextTests{ ??? protected FooService fooService; ??? ??? public MyTest(){ ?????? //啟用直接對屬性變量進行注入的機制 this.setPopulateProtectedVariables(true); ??? } ??? protected String[] getConfigLocations(){ ?????? return new String[]{"spring-config-beans.xml"}; ??? } ??? public void testSave(){ ?????? String str = this.fooService.save("Tony"); ?????? System.out.print(str); ?????? assertEquals("success", str); ??? } } 代碼清單2中我們移除了set方法,增加了一個構造函數,在構造函數中調用父類的方法啟用直接對屬性變量進行注入的機制。有時我們測試的時候會操作數據庫插入一條記錄,由于我們不會每次都修改測試的數據,當我們再次插入同樣的數據時數據庫肯定會要報錯了,此時我們需要既能測試又能不讓測試的數據在數據庫中起作用,Spring就知道我們的這個需要,為我們準備了AbstractTransactionalSpringContextTests這個類。 代碼清單3 import org.springframework.test. AbstractTransactionalSpringContextTests; import com.tony.web.service.FooService; public class MyTest extends AbstractTransactionalSpringContextTests{ ??? protected FooService fooService; ??? public MyTest(){ ?????? this.setPopulateProtectedVariables(true); ??? } ??? protected String[] getConfigLocations(){ ?????? return new String[]{"spring-config-beans.xml"}; ??? } //測試方法中的數據操作將在方法返回前被回滾,不會對數據庫產生永久性數據操作,下一//次運行該測試方法時,依舊可以成功運行. ??? public void testSave(){ ?????? String str = this.fooService.save("Tony"); ?????? System.out.print(str); ?????? assertEquals("success", str); ??? } } ??? 這樣就可以在方法返回之前將測試數據回滾,以保證下次單元測試的成功。總結
以上是生活随笔為你收集整理的Spring应用的单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring 2.5 基于注解驱动的 S
- 下一篇: oracle查询指定行数间的记录