springboot集成測試最小化依賴實踐示例
簡介
想要代碼跑的穩(wěn), 集成測試還是必不可少的, 不然出現(xiàn)開發(fā)環(huán)境正常, 集成環(huán)境各種問題就坑爹了。
當前項目對外提供各種rest接口, 通過RestTemplate做接口測試, 同時需要注入一些SpringBean, 如何使用SpringBootTest又不需要啟動整個容器?
版本及依賴引入
springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>項目部分依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>直接使用SpringBootTest方式
代碼示例
@RunWith(SpringRunner.class)
// 默認啟動容器
@SpringBootTest
public class BdgResourceITest {
@Autowired
@Qualifier(value = "iTestRestTemplate")
private RestTemplate restTemplate;
@Test
public void testPull() throws URISyntaxException {
// /pull/{mof_div_code}/{fiscal_year}/{agency_code}
String url = "/api/pull/340000000/2022/001001";
final ResponseEntity<ResponseData> exchange = restTemplate.exchange(
RequestEntity.get(new URI(url)).build(), ResponseData.class);
Assert.isTrue(exchange.getStatusCode().equals(HttpStatus.OK), "本單位數(shù)據(jù)獲取異常");
}
}場景及優(yōu)劣
優(yōu)勢
如果是測試類中大量引入了依賴, 這種情況下直接啟動容器比較方便, 不過集成測試個人感覺從入口訪問即可, 這種嵌套比較深的建議使用單元測試
劣勢
當前項目中測試代碼需要依賴很少, 極端情況下只用restTemplate即可, 根本沒必要啟動容器, 而且啟動容器占用了大量時間
項目中使用了ehcache3.x作為本地緩存, 啟動容器后因為文件鎖無法測試, 如果單獨指定ehcache.xml配置, 又會產(chǎn)生新的垃圾, 所以果斷減少依賴
最小化依賴方案
代碼
@RunWith(SpringRunner.class)
// 指定class就不啟動容器了
@SpringBootTest(classes = BdgResourceITest.class)
@Import(value = {ITestRestTemplateConfigurer.class})
// 激活 main 中resources下的test profile
//@ActiveProfiles("dev")
// 加載測試目錄resources下的application.yml文件
//@TestPropertySource(properties = {"spring.config.location=classpath:application.yml"})
public class BdgResourceITest {
@Autowired
@Qualifier(value = "iTestRestTemplate")
private RestTemplate restTemplate;
@Test
public void testPull() throws URISyntaxException {
// /pull/{mof_div_code}/{fiscal_year}/{agency_code}
String url = "/api/pull/340000000/2022/001001";
final ResponseEntity<ResponseData> exchange = restTemplate.exchange(
RequestEntity.get(new URI(url)).build(), ResponseData.class);
Assert.isTrue(exchange.getStatusCode().equals(HttpStatus.OK), "本單位數(shù)據(jù)獲取異常");
}
}思路及步驟
通過指定SpringBootTest的classes, 只啟動當前類,如果需要注入其它bean, 則使用@import進行引入
如果import內(nèi)部的類也也需要引入其它類, 同理根據(jù)需要使用@Import注解, 這樣產(chǎn)生的代碼更加聚合, 所然在當前類可以全部@Import, 但是看著頭疼
對于需要引入yml配置信息的,可以配合@EnableConfigurationProperties讀取測試目錄下的application.yml文件
最小化依賴方案的優(yōu)點
減少了容器啟動時間, 對于當前項目更加符合實際的使用場景, 畢竟第三方使用不可能啟動你自己的容器:D
更加優(yōu)雅的解決了ehcache同時被容器掃描啟動, 本地文件鎖導致測試無法運行, 實際測試代碼根本不需要緩存, 項目服務有就行
測試代碼也更加簡單優(yōu)雅, 可以直接提供第三方公司作為接口請求示例代碼
結(jié)論
如果集成測試的場景類似當前項目情況, 全部測試都從rest接口入手, 建議采用最小容器依賴方案
以上就是springboot集成測試最小化依賴實踐示例的詳細內(nèi)容,更多關(guān)于springboot集成測試依賴的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
RocketMQ?NameServer架構(gòu)設計啟動流程
這篇文章主要為大家介紹了RocketMQ?NameServer架構(gòu)設計啟動流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

