【发布时间】:2021-05-18 14:19:16
【问题描述】:
我想为客户端服务创建单元测试。
客户端服务的作用是调用webservice,获取数据,按时更新数据库。
计划的方法返回 void。
如何为 a 创建单元测试
- 客户服务
- 调度方法
- 无效返回方法
客户端是这样的:
@ApplicationScoped
public class ClientClass {
private static final Logger LOGGER = Logger.getLogger(VdsClient.class);
@Inject
Client client;
VMR vmr;
CommandService commandService;
public VdsClient(VMR vmr,
CommandService commandService) {
this.vmr = vmr;
this.commandService = commandService;
}
@Scheduled(XXX)
public void getVal() {
var monitoringStateFilter =
new VMF.vmf(true, true);
var monoResultList =
vmr.fvms(monitoringStateFilter)
.collectList();
if (monoResultList != null) {
var resultList = monoResultList.block();
if (resultList != null) {
resultList.stream()
.map(row -> row.getValue("val", val.class))
.map(vin -> this.updateEstimate(val.getValue()))
}
}
}
public Tuple2<String, Boolean> updateEstimate(String val) {
List<route> routeList;
try {
routeList = vdsClient.getval(val)
.getItem();
boolean hasDealerDestination = false;
for (Route route : routeList) {
if (vd.DestLocationType._00.value()
.equals(route.getTransportConnectionPointTyp())) {
hasDealerDestination = true;
var estimate = DateTimeUtil.convertToInstantWithOffset(route.getArrivalDate(),
route.getArrivalTime(), route.getTimezone(), DateTimeUtil.PATTERN_LONG_DATE);
if (estimate == null) {
return Tuples.of(val, false);
}
var result = this.updateVehicleEstimate(val, estimate);
return Tuples.of(val, result);
}
}
if (!hasDealerDestination) {
return Tuples.of(val, false);
} else {
return Tuples.of(val, false);
}
} catch (route e) {
return Tuples.of(val, false);
} catch (Exception e) {
return Tuples.of(val, false);
}
}
public Boolean updateVehicleEstimate(String val, Instant estimate) {
var vehicleUpdate = vu.vuc.builder()
.val(new Val(val))
.Estimate(estimate)
.build();
return (Boolean) cs.ec(vu).block();
}
【问题讨论】:
-
单元测试实际上是为了确认在给定某些输入的情况下单元的外部影响是否按预期发生。外部影响可以是返回值和状态修改。那么,既然你调用了这个方法,你如何确保状态修改发生了呢?您需要的是一个模拟/线束来表示正在调用的外部服务,并确保某些突变按照您的预期发生或没有发生。要么模拟你的 web 服务客户端的某些部分,要么使用类似 wiremock 的东西来模拟外部 web 服务。
标签: java junit mockito junit5 quarkus