【发布时间】:2021-08-05 18:05:38
【问题描述】:
我来这里是因为我还没有找到解决问题的方法。我实际上是在尝试使用wiremock 来存根响应(对Mocked 服务的调用是通过FeignClient 完成的)。我的意图是在真实的应用程序中而不是在测试中,而不是在真实的应用程序中获得真实的伪装客户端的虚假响应。因此,在这种情况下,我不是在测试中存根 WireMockServer,而是在 Spring Boot 应用程序类中存根,但是当在那里存根响应时,我遇到了一个非常奇怪的错误,到目前为止我已经调查了很多但没有成功。
这是我目前使用的代码:
@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:8000\n\n" );
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 );
}
}
这实际上在stubFor( mappingBuilder ); 行中失败了,这是我得到的例外:
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
所以一个问题是,wiremock 是否只在测试中起作用?
我已尝试更改 JSON 及其字段,但这似乎不起作用,所以如果你们中的任何人知道如何解决该问题会有所帮助,或者如果您也知道如何存根被调用的 api 请求@FeignClient(不是在测试中,而是在真正的 Spring 应用程序运行中)作为替代方案可能也可以工作。
谢谢!
【问题讨论】:
-
这是非常奇怪的行为。没有理由不能将 WireMock 与您的生产 Spring 应用程序一起运行 - 我之前已经看过很多次了。一个假设是,另一个依赖项正在以某种方式在全局范围内更改 Jackson 配置。如果是这种情况,切换到独立 JAR 可能会阻止它发生(因为杰克逊在那个 JAR 中被遮住了)。
标签: java spring-boot wiremock stubbing jsonexception