Java单元测试Mock接口模拟调用其中某个方法

java 复制代码
@Test
void bondCalculate() {
	Bc6CalculateRequestDto requestDto = new Bc6CalculateRequestDto();
	requestDto.setTradeDate(LocalDate.now());
	requestDto.setSpeed(SpeedEnum.T_1.getSpeed());
	requestDto.setCalcType(Strings.concat(ModifyFieldEnum.RATE.getField(), ModifyFieldEnum.FIRST_AMOUNT.getField()));
	//
	Bc6CalculateServiceImpl bc6CalculateService = Mockito.spy(new Bc6CalculateServiceImpl());
	String allTradeDateListStr = ExcelUtil.excelToJsonStr("bc6/NibTradeDate.xlsx");
	List < NibTradeDateCloudDto > allTradeDateList = ObjectMapperUtil.parseArray(allTradeDateListStr, NibTradeDateCloudDto.class);
	Mockito.doReturn(allTradeDateList).when(bc6CalculateService).getAllTradeDateList();
	//
	Bc6CalcResponseDto responseDto = bc6CalculateService.bondCalculate(requestDto);
	System.out.println(responseDto.toString());
}

这个测试方法模拟了 Bc6CalculateService 的一个实例,并试图测试 bondCalculate 方法。它使用了 Mockito.spy 来创建一个真实对象的部分模拟(间谍),并模拟了 getAllTradeDateList 方法的行为。然后,它调用了 bondCalculate 方法并打印了返回的 Bc6CalcResponseDto 对象的字符串表示。

相关推荐
霸王大陆几秒前
《零基础学 PHP:从入门到实战》教程-模块四:数组与函数-1
android·开发语言·php
APIshop2 分钟前
Java爬虫第三方平台获取1688关键词搜索接口实战教程
java·开发语言·爬虫
k***12173 分钟前
SpringCloud实战【九】 SpringCloud服务间调用
java·spring boot·spring cloud
请为小H留灯6 分钟前
Java快捷健(详细版)
java·开发语言
执笔论英雄7 分钟前
【RL】 ROLL Generate Scheduler
java·服务器·数据库
北郭guo9 分钟前
垃圾回收底层原理【深入了解】
java·jvm·算法
小年糕是糕手9 分钟前
【C++同步练习】C++入门
开发语言·数据结构·c++·算法·pdf·github·排序算法
D***441413 分钟前
【SpringBoot】Spring Boot 项目的打包配置
java·spring boot·后端
5***E68513 分钟前
Spring Boot接收参数的19种方式
java·spring boot·后端
u***B79214 分钟前
Spring Boot的项目结构
java·spring boot·后端