-
Notifications
You must be signed in to change notification settings - Fork 436
Description
Spring cloud version 2025.1.0
When using @AutoConfigureStubRunner from org.springframework.cloud:spring-cloud-starter-contract-stub-runner, if you have org.wiremock.integrations:wiremock-spring-boot:4.0.8 in the classpath, you get this error:
Caused by: java.lang.ClassCastException: class org.springframework.cloud.contract.verifier.builder.handlebars.HandlebarsJsonPathHelper cannot be cast to class com.github.jknack.handlebars.Helper (org.springframework.cloud.contract.verifier.builder.handlebars.HandlebarsJsonPathHelper and com.github.jknack.handlebars.Helper are in unnamed module of loader 'app')
at com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine.addHelpers(TemplateEngine.java:113)
at com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine.<init>(TemplateEngine.java:77)
at com.github.tomakehurst.wiremock.extension.Extensions.configureTemplating(Extensions.java:144)
at com.github.tomakehurst.wiremock.extension.Extensions.load(Extensions.java:113)
at com.github.tomakehurst.wiremock.core.WireMockApp.<init>(WireMockApp.java:108)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:68)
at org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStub.start(WireMockHttpServerStub.java:138)
at org.springframework.cloud.contract.stubrunner.StubServer.start(StubServer.java:51)
at org.springframework.cloud.contract.stubrunner.StubRunnerExecutor.startStubServers(StubRunnerExecutor.java:299)
at org.springframework.cloud.contract.stubrunner.StubRunnerExecutor.runStubs(StubRunnerExecutor.java:95)
at org.springframework.cloud.contract.stubrunner.StubRunner.runStubs(StubRunner.java:77)
at org.springframework.cloud.contract.stubrunner.BatchStubRunner.runStubs(BatchStubRunner.java:47)
at org.springframework.cloud.contract.stubrunner.spring.StubRunnerConfiguration.batchStubRunner(StubRunnerConfiguration.java:88)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:155)
... 62 more
As I understand, this happens because com.github.tomakehurst.wiremock.extension.responsetemplating.TemplateEngine is taken from regular wiremock dependency, which expects com.github.jknack.handlebars.Helper, but spring cloud HandlebarsJsonPathHelper uses shaded wiremock.com.github.jknack.handlebars.Helper
For those who encountered the same issue the fix for the time being would be to exclude wiremock from the org.wiremock.integrations:wiremock-spring-boot:4.0.8 like that:
testImplementation("org.wiremock.integrations:wiremock-spring-boot:4.0.8") {
exclude group: 'org.wiremock', module: 'wiremock'
exclude group: 'org.wiremock', module: 'wiremock-jetty12'
}
so TemplateEngine is taken from spring cloud's instead.
Sample dependencies:
testImplementation "org.wiremock.integrations:wiremock-spring-boot:4.0.8"
testImplementation "org.springframework.cloud:spring-cloud-starter-contract-stub-runner"
Sample class:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = ExampleApplication.class)
@AutoConfigureWebMvc
@AutoConfigureJsonTesters
@AutoConfigureStubRunner(ids = "com.example:rest-api:+:stubs:8099")
@ActiveProfiles("TEST")
class RestApiContractTest { }