here I come because I have not found any solution to my problem. I’m actually trying to stub a response with wiremock (that call to the Mocked service is be done via FeignClient). My intention is to get a fake response with the real feign client, not in a test, but in the real application. Therefore, In this case, I’m not stubbing the WireMockServer in a test, but in the spring boot application class, however when stubbing there the response I’m having a super weird error that I have investigated a lot so far without success.
Here is the code I’m using so far:
@EnableFeignClients @SpringBootApplication public class CrmApplication implements CommandLineRunner { private final ConfigurableApplicationContext context;private final ConfigurableApplicationContext context; @Autowired public CrmApplication( ConfigurableApplicationContext context ) { this.context = context; } public static void main( String[] args ) { log.info( "Starting the CRM Validator" ); SpringApplication.run( CrmApplication.class, args ); log.info( "Finishing the CRM Validator" ); } @Override public void run( String... args ) { final WireMockServer server = new WireMockServer( options().port( 8000 ) ); server.start(); log.info( "Wiremock has started in the following url: {}", "http://localhost:8000nn" ); String resultJson = "{"id":1,"hasJudicialRecords":false}"; MappingBuilder mappingBuilder = get( urlPathEqualTo( "/api/v1/judicial-registry/1") ) .willReturn( aResponse().withStatus( 200 ) .withHeader( "Content-Type", "application/json" ) .withBody( resultJson ) ) stubFor( mappingBuilder ); } }
This is actually failing in the stubFor( mappingBuilder );
line, and this is the exception I got:
java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:822) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:803) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:346) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) ~[spring-boot-2.4.5.jar:2.4.5] at com.crm.demo.CrmApplication.main(CrmApplication.java:31) ~[main/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.4.5.jar:2.4.5] Caused by: com.github.tomakehurst.wiremock.common.JsonException: { "errors" : [ { "code" : 10, "source" : { "pointer" : "/timestamp" }, "title" : "Error parsing JSON", "detail" : "Unrecognized field "timestamp" (class com.github.tomakehurst.wiremock.common.Errors), not marked as ignorable" } ] } at com.github.tomakehurst.wiremock.common.JsonException.fromJackson(JsonException.java:53) ~[wiremock-jre8-2.28.0.jar:na] at com.github.tomakehurst.wiremock.common.Json.read(Json.java:55) ~[wiremock-jre8-2.28.0.jar:na] at com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:486) ~[wiremock-jre8-2.28.0.jar:na] at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:454) ~[wiremock-jre8-2.28.0.jar:na] at com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:131) ~[wiremock-jre8-2.28.0.jar:na] at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:298) ~[wiremock-jre8-2.28.0.jar:na] at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:293) ~[wiremock-jre8-2.28.0.jar:na] at com.github.tomakehurst.wiremock.client.WireMock.givenThat(WireMock.java:104) ~[wiremock-jre8-2.28.0.jar:na] at com.github.tomakehurst.wiremock.client.WireMock.stubFor(WireMock.java:108) ~[wiremock-jre8-2.28.0.jar:na] at com.crm.demo.CrmApplication.run(CrmApplication.java:31) ~[main/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:819) ~[spring-boot-2.4.5.jar:2.4.5] ... 10 common frames omitted
So one question would be, does wiremock only work in testing?
I have tried changing the JSON, and its fields but that does not seem to work, so if any of you know whow to solve that issue would be helpful, or if you also know how to stub an api request that is called by @FeignClient (not in a test but in the real spring application run) as an alternative maybe could work too.
Thank you!.
Advertisement
Answer
Right after server.start()
you need:
configureFor("localhost", server.port());