Spring Test 整合 JUnit 4 使用

2021-09-08 19:40:58 字數 715 閱讀 2665

使用spring的測試框架需要加入以下依賴包:

spring test (spring框架中的test包)

spring 相關其他依賴包(不再贅述了,就是context等包)

在此,推薦建立乙個和src平級的源檔案目錄,因為src內的類都是為日後產品準備的,而此處的類僅僅用於測試。而包的名稱可以和src中的目錄同名,這樣由於在test源目錄中,所以不會有衝突,而且名稱又一模一樣,更方便檢索。

建立乙個測試用的類,推薦名稱為 「被測試類名稱 + test」。

測試類應該繼承與 abstractjunit4springcontexttests 或 abstracttransactionaljunit4springcontexttests

對於 abstractjunit4springcontexttests 和 abstracttransactionaljunit4springcontexttests 類的選擇:

如果沒有新增上述bean,將會丟擲nosuchbeandefinitionexception,指明 no bean named 'transactionmanager' is definded.

建立測試用方法,推薦名稱為 「被測方法名稱+ test」。

測試方法上方加入 @test

右鍵方法名,選擇則「run as」→「junit test」即可

ok,到此為止就可以使用spring的測試框架了。

Spring Test 整合 JUnit 4 使用

使用spring的測試框架需要加入以下依賴包 spring test spring框架中的test包 spring 相關其他依賴包 不再贅述了,就是context等包 在此,推薦建立乙個和src平級的源檔案目錄,因為src內的類都是為日後產品準備的,而此處的類僅僅用於測試。而包的名稱可以和src中的...

Junit 與 spring test 結合使用

方式一 junit依賴包 junit junit 4.12 這個方法不使用spring test支援,需要在 before 的方法中構建spring上下文如下 before public void before throws exception 我們發現,當載入的檔案或者注入的實體過多時before...

Junit測試的羽翼Spring test

單元測試的作用無需多講,像sonarqube這些 質量管理軟體也把單元測試覆蓋率作為乙個重要的指標來衡量系統 質量,單元測試 覆蓋率在某種程度上反應了相應 的可靠性。使用慣了spring的依賴注入的朋友相信也體驗到了這個特性帶來的便利性,只需在相應的service實現上加上 service註解,在x...