Framework/Spring Mock
Framework
Contract-driven WireMock support for ApiHug Spring integration tests.
it-common-spring-mock turns generated ApiHug contract metadata into a WireMock-based upstream mock server for tests. Instead of hand-writing large stub suites, it reads the generated Module metadata, registers HTTP stubs automatically, and produces mock payloads from the proto contract.
@AutoConfigureWireMockUse @AutoConfigureWireMock on a Spring test class to start and manage the WireMock server.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWireMock(port = 0)
class UserServiceTest {
}
port = 0 is the safest default because it avoids port conflicts in CI and local runs.
wiremock.server.*The extension is configured through the wiremock prefix.
| Property | Default | Meaning |
|---|---|---|
wiremock.server.port | 8080 | HTTP port |
wiremock.server.https-port | -1 | HTTPS port, disabled when negative |
wiremock.server.port-dynamic | false | Use a random HTTP port |
wiremock.server.https-port-dynamic | false | Use a random HTTPS port |
wiremock.server.stubs | [] | Extra stub directories |
wiremock.server.files | [] | Response body file directories |
wiremock.reset-mappings-after-each-test | false | Reset mappings after each test |
wiremock.rest-template-ssl-enabled | false | Enable SSL support for RestTemplate |
For most ApiHug tests, @AutoConfigureWireMock(port = 0) plus ${wiremock.server.port} is enough.
HopeContractConfigurationHopeContractConfiguration is the contract bridge. Extend it and return the generated upstream Module. The framework will walk its services and methods and register WireMock stubs from the proto-derived HTTP metadata.
@Configuration
public class UpstreamServiceMockConfig extends HopeContractConfiguration {
@Override
protected Module module() {
return UpstreamApiModule.INSTANCE;
}
@Override
protected RuntimeContext runtimeContext() {
RuntimeContext context = super.runtimeContext();
context.setResultPlain(true);
return context;
}
}
That gives you:
WireMockStubCustomizerUse WireMockStubCustomizer when a specific scenario must override or extend the contract-generated defaults.
@Component
public class ErrorScenarioStub implements WireMockStubCustomizer {
@Override
public String name() {
return "error-scenario";
}
@Override
public void stub(WireMockServer server) {
server.stubFor(
get(urlPathEqualTo("/api/users/999"))
.willReturn(aResponse().withStatus(404))
);
}
}
This is the right extension point for edge cases such as 404, 500, throttling, or domain-specific negative flows.
WireMockConfigurationCustomizerIf you need lower-level WireMock server tuning, use WireMockConfigurationCustomizer.
@Bean
public WireMockConfigurationCustomizer myConfigCustomizer() {
return config -> config.timeout(5000);
}
it-common-spring-mock in test scope.@AutoConfigureWireMock.HopeContractConfiguration classes for the upstream services you need to fake.${wiremock.server.port} into downstream client configuration.WireMockStubCustomizer only when the contract-generated stub is not enough.@SpringBootTest
@AutoConfigureWireMock(port = 0)
@Import(UpstreamServiceMockConfig.class)
class OrderServiceIntegrationTest {
@Autowired
private OrderService orderService;
@Test
void shouldCreateOrder() {
OrderDTO order = orderService.create(new CreateOrderRequest().setUserId(1L));
assertThat(order).isNotNull();
}
}
it-common-spring-plus/it-common-spring-mock/README.mdhope.common.mock.server.wiremock.AutoConfigureWireMockhope.common.mock.server.wiremock.HopeContractConfigurationhope.common.mock.server.wiremock.WireMockStubCustomizer