javascript
【SpringBoot Testing】测试类/测试用例格式
這里寫目錄標題
- 前言
- Test Scope Dependencies
- 編寫測試用例
- 測試用例該放哪個`package`中
前言
- springboot 2.3.5.RELEASE
Test Scope Dependencies
需要引入“spring-boot-starter-test”:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency>“spring-boot-starter-test”啟動器包含以下庫:
- JUnit:單元測試 Java 應用程序的事實標準。
- Spring 測試和 Spring Boot 測試:對 Spring Boot 應用程序的實用程序和集成測試支持。
- AssertJ:一個流暢的斷言庫。
- Hamcrest:匹配器對象庫(也稱為約束或謂詞)。
- Mockito:Java模擬框架。
- JSONassert:JSON 的斷言庫。
- JsonPath : JSON 的 XPath。
“spring-boot-starter-test”啟動器包含 vintage engine,兼容 Junit 4 和 Junit 5 的測試用例。如果你的項目中沒有 Junit 4 的測試用例時,去掉 vintage engine 吧:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions> </dependency>編寫測試用例
package com.mydomain.myproject;import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest public class TestSpringBoot {@Testpublic void testCase01() {} }-
package com.mydomain.myproject; : 測試用例的所處的package不能亂寫。否則,會出現錯誤java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
-
@SpringBootTest : 使用 Junit 4 時,需要搭配 @RunWith(SpringRunner.class) 使用。Junit 5 不需要這樣。
-
@Test :在測試用例上添加 @Test ,讓 Junit 能夠找到它。
Junit 4 時這樣寫:
package com.mydomain.myproject;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) @SpringBootTest public class TestSpringBoot {@Testpublic void testCase01() {} }測試用例該放哪個package中
假設項目的目錄結構如下:
MyProject ├─src │ ├─main │ │ ├─java │ │ │ └─com │ │ │ └─mydomain │ │ │ └─myproject │ │ │ ├─SpringbootApplication.java │ │ │ └─... │ │ └─resources │ │ └─... │ └─test │ ├─java │ │ └─com │ │ └─mydomain │ │ ├─TestSpringBoot1.java │ │ └─myproject │ │ ├─TestSpringBoot2.java │ │ └─service │ │ └─TestSpringBoot3.java │ └─resources └─POM.xml- TestSpringBoot1.java 運行時,會出現錯誤 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test。
- TestSpringBoot2.java 運行時,不會出先錯誤。
- TestSpringBoot3.java 運行時,不會出先錯誤。
這跟 Spring Boot Test 檢測配置的方式有關。它會嘗試尋找 @SpringBootApplication 或 @SpringBootConfiguration。為此需要滿足幾個條件:
- 你的項目的代碼結構符合 Spring Boot 代碼結構
- 從測試用例所在的 package 開始尋找,并逐層向上尋找。直到尋找 @SpringBootApplication 或 @SpringBootConfiguration 為止。
因此,從 TestSpringBoot1.java 所在目錄開始向上尋找時,找不到 @SpringBootApplication 或 @SpringBootConfiguration。所以,會出現錯誤 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的【SpringBoot Testing】测试类/测试用例格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Mybatis】sqlSessionT
- 下一篇: 怎么把彩色的照片变黑白色怎么把彩色的照片