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

使用手动创建的存根对接Spring Cloud Contract Stub Runner

Is Spring Cloud Contract Suitable for Your Scenario?

Absolutely, your use case is not only feasible but highly recommended with Spring Cloud Contract! You don’t have to rewrite your existing WireMock JSON stubs—Spring Cloud Contract’s Stub Runner has native support for WireMock stubs, letting you reuse your existing work while leveraging all the benefits of Spring Cloud Contract for new features.

Why This Works & Why It’s a Good Idea

Spring Cloud Contract is built to be flexible, supporting multiple stub formats (including WireMock’s JSON, plus its own YAML/Groovy contracts). This makes it perfect for incremental migrations like yours:

  • Keep using your existing WireMock stubs for legacy functionality without rewriting a line
  • For new features, adopt Spring Cloud Contract’s contract-first approach with YAML/Groovy to generate stubs and enforce API consistency
  • Stub Runner unifies your stub management, offering features like dynamic port allocation, centralized stub discovery, and consistent test setup across your team

Configuring Stub Runner to Use Local WireMock Stubs

To get your existing local WireMock stubs working with @AutoConfigureStubRunner, use this configuration:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
@AutoConfigureMockMvc
@AutoConfigureStubRunner(
    stubsMode = StubRunnerProperties.StubsMode.LOCAL,
    stubsLocations = "classpath:**/stubs"
)
public class MyNewClientIT {
    // Your test logic here
}

Let’s break down the key parameters:

  • stubsMode = StubRunnerProperties.StubsMode.LOCAL: Tells Stub Runner to load stubs directly from your local classpath, no need to pull from remote artifact repositories (since your stubs aren’t published as artifacts yet)
  • stubsLocations = "classpath:**/stubs": Points to the root directory of your existing WireMock stubs. Stub Runner will automatically scan this path (and all subdirectories) for WireMock’s mappings/*.json files, just like your original WireMock setup did.

Optional: Fine-Tuning Stub Loading

If you need more control, adjust the configuration to fit your setup:

  • To use a fixed port (like your original 8989), add port = 8989 to @AutoConfigureStubRunner
  • If your stubs are organized by service (e.g., classpath:stubs/my-service), narrow the stubsLocations path to that specific directory
  • For multiple local stub sources, separate paths with commas: stubsLocations = "classpath:stubs/service-a, classpath:stubs/service-b"

Key Notes

  • Stub Runner spins up a WireMock server under the hood, so your client code interacts with it exactly like it did with your original WireMock setup
  • You can mix and match: existing WireMock stubs and new Spring Cloud Contract-generated stubs work seamlessly together
  • This approach lets you gradually migrate to full contract-first testing without discarding your existing test assets

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:14:34