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

无需PowerMockito,如何在单元测试中Mock Google Workflow客户端调用?

可行方案汇总

1. 封装静态调用到自定义服务层(最推荐)

把直接调用ExecutionsClient.create()的逻辑封装成独立的Spring Bean,业务代码只依赖这个自定义服务,彻底避开静态方法的Mock问题,同时符合依赖倒置原则。

示例代码:

// 自定义Workflow执行服务
@Service
public class GoogleWorkflowExecutor {
    public void triggerWorkflow(String workflowId, String projectId, String location) {
        try (ExecutionsClient executionsClient = ExecutionsClient.create()) {
            ExecutionName parent = ExecutionName.of(projectId, location, workflowId);
            Execution execution = Execution.newBuilder().build();
            executionsClient.createExecution(parent, execution);
        } catch (IOException e) {
            throw new RuntimeException("触发Google Workflow失败", e);
        }
    }
}

// 业务类注入使用
@Service
public class YourBusinessService {
    private final GoogleWorkflowExecutor workflowExecutor;

    public YourBusinessService(GoogleWorkflowExecutor workflowExecutor) {
        this.workflowExecutor = workflowExecutor;
    }

    public void processBusinessTask() {
        workflowExecutor.triggerWorkflow("order-process", "my-gcp-project", "us-central1");
    }
}

// 单元测试
@SpringBootTest
public class YourBusinessServiceTest {
    @MockBean
    private GoogleWorkflowExecutor workflowExecutor;

    @Autowired
    private YourBusinessService businessService;

    @Test
    void testProcessBusinessTask() {
        businessService.processBusinessTask();
        // 只需验证自定义服务的方法被调用即可
        verify(workflowExecutor).triggerWorkflow(anyString(), anyString(), anyString());
    }
}

2. 使用Mockito-inline Mock静态方法(适合少量修改代码的场景)

Mockito 3.4+版本开始支持inline模式,无需PowerMockito就能直接Mock静态方法。

步骤:

  • 添加Maven依赖:
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>4.11.0</version>
    <scope>test</scope>
</dependency>
  • 测试代码示例:
@Test
void testTriggerWorkflow() throws IOException {
    // 用try-with-resources包裹静态Mock,确保测试后清理状态
    try (MockedStatic<ExecutionsClient> mockedClient = Mockito.mockStatic(ExecutionsClient.class)) {
        ExecutionsClient mockClient = Mockito.mock(ExecutionsClient.class);
        mockedClient.when(ExecutionsClient::create).thenReturn(mockClient);

        // 执行业务逻辑
        yourBusinessService.processBusinessTask();

        // 验证静态方法和客户端方法的调用
        mockedClient.verify(ExecutionsClient::create, times(1));
        verify(mockClient).createExecution(any(ExecutionName.class), any(Execution.class));
    }
}

3. 依赖注入ExecutionsClient(替换静态创建逻辑)

修改代码,将ExecutionsClient作为Spring Bean注入,而非直接调用静态create()方法,测试时用@MockBean替换真实客户端即可。

示例代码:

// GCP客户端配置类
@Configuration
public class GoogleCloudConfig {
    @Bean
    public ExecutionsClient executionsClient() throws IOException {
        return ExecutionsClient.create();
    }
}

// 业务类注入使用
@Service
public class YourBusinessService {
    private final ExecutionsClient executionsClient;

    public YourBusinessService(ExecutionsClient executionsClient) {
        this.executionsClient = executionsClient;
    }

    public void triggerWorkflow(String workflowId, String projectId, String location) {
        ExecutionName parent = ExecutionName.of(projectId, location, workflowId);
        Execution execution = Execution.newBuilder().build();
        executionsClient.createExecution(parent, execution);
    }
}

// 单元测试
@SpringBootTest
public class YourBusinessServiceTest {
    @MockBean
    private ExecutionsClient executionsClient;

    @Autowired
    private YourBusinessService businessService;

    @Test
    void testTriggerWorkflow() {
        businessService.triggerWorkflow("test-workflow", "test-project", "us-central1");
        verify(executionsClient).createExecution(any(ExecutionName.class), any(Execution.class));
    }
}

内容的提问来源于stack exchange,提问作者psv

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 23:16:17