簡體   English   中英

Wiremock 存根錯誤:“無法識別的字段 \"timestamp\"(類 com.github.tomakehurst.wiremock.common.Errors),未標記為可忽略”

[英]Wiremock stubbing error: "Unrecognized field \"timestamp\" (class com.github.tomakehurst.wiremock.common.Errors), not marked as ignorable"

我來這里是因為我還沒有找到任何解決我問題的方法。 我實際上是在嘗試使用wiremock 來存根響應(對Mocked 服務的調用是通過FeignClient 完成的)。 我的意圖是在真實的應用程序中而不是在測試中,而不是在真實的應用程序中獲得真實的偽裝客戶端的虛假響應。 因此,在這種情況下,我沒有在測試中對 WireMockServer 進行存根,而是在 spring 引導應用程序 class 中存根,但是當存根響應時,我遇到了一個超級奇怪的錯誤,到目前為止我已經調查了很多但沒有成功。

這是我到目前為止使用的代碼:

@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 及其字段,但這似乎不起作用,所以如果你們中的任何人知道如何解決該問題會有所幫助,或者如果您還知道如何存根由 @FeignClient 調用的 api 請求(不是在測試中,而是在真正的 spring 應用程序運行中)作為替代方案可能也可以工作。

謝謝..

server.start()之后,您需要:

configureFor("localhost", server.port());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM