You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何测试调用Getter获取对象方法的方法?DynamoDB网关测试遇阻

问题分析与解决方案

核心问题

你的update方法存在两个关键问题:

  1. 异步操作未与返回Mono关联:调用updateItem返回的CompletableFuture没有被订阅或串联到返回的Mono中,导致DynamoDB的更新操作可能在后台静默执行(甚至可能因为未被订阅而不执行),且操作中的异常完全无法传递到上层调用方。
  2. 泛型类型不匹配:UpdateItemEnhancedRequest的泛型写的是PhysicalcardCustomerInfoEntity,但构建器用的是CustomerEntity.class,类型不一致会引发潜在问题。

修复后的Update方法

@Override
public Mono<Boolean> update(CustomerInfo info) {
    CustomerEntity customerEntity = new CustomerEntity();
    customerEntity.setCustomerId(info.getCustomerId());
    customerEntity.setName(info.getName());
    customerEntity.setLastName(info.getLastName());
    customerEntity.setBirthDate(info.getBirthDate());
    
    Expression itemExistsExpression = Expression.builder()
            .expression("attribute_exists(customerId)")
            .build();

    // 修正泛型类型为CustomerEntity
    UpdateItemEnhancedRequest<CustomerEntity> updateItemEnhancedRequest = UpdateItemEnhancedRequest
            .builder(CustomerEntity.class)
            .item(customerEntity)
            .conditionExpression(itemExistsExpression)
            .ignoreNulls(true)
            .build();

    // 将CompletableFuture转换为Mono,并串联操作逻辑
    return Mono.fromFuture(() -> this.customerAdapter.getCustomerTable().updateItem(updateItemEnhancedRequest))
            .doOnSuccess(unused -> log.info("Updated in DynamoDB!"))
            .thenReturn(true)
            .onErrorMap(e -> new RuntimeException("Failed to update customer", e));
}

修正后的测试用例

需要分别测试成功和失败场景,同时验证updateItem是否被正确调用:

@Test
void updateCustomerInfo_Success() {
    // 模拟DynamoDB更新成功
    Mockito.when(customerAdapter.getCustomerTable().updateItem(any(UpdateItemEnhancedRequest.class)))
            .thenReturn(CompletableFuture.completedFuture(null));

    Mono<Boolean> infoMono = customerGateway.update(dynamoData.get(0));
    StepVerifier.create(infoMono)
            .expectNext(true)
            .expectComplete()
            .verify();

    // 验证updateItem方法确实被调用
    Mockito.verify(customerAdapter.getCustomerTable()).updateItem(any(UpdateItemEnhancedRequest.class));
}

@Test
void updateCustomerInfo_Failure_WhenItemNotFound() {
    // 模拟条件校验失败(比如customerId不存在)的异常
    ConditionalCheckFailedException exception = new ConditionalCheckFailedException("Target customer does not exist");
    Mockito.when(customerAdapter.getCustomerTable().updateItem(any(UpdateItemEnhancedRequest.class)))
            .thenReturn(CompletableFuture.failedFuture(exception));

    Mono<Boolean> infoMono = customerGateway.update(dynamoData.get(0));
    StepVerifier.create(infoMono)
            .expectErrorMatches(e -> e instanceof RuntimeException && e.getMessage().contains("Failed to update customer"))
            .verify();
}

关键说明

  • 用Mono.fromFuture包装CompletableFuture,让异步操作纳入Reactor的响应式流管理,确保操作会被执行,且异常能被捕获处理。
  • doOnSuccess用于执行日志记录,不会影响流的返回值。
  • thenReturn(true)在更新成功后返回预期的布尔值,onErrorMap将DynamoDB的异常转换为业务异常向上传递。

内容的提问来源于stack exchange,提问作者Eduardo Reusche Sáenz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 22:09:24