2017-12-27 7 views
2

übereinstimmen Ich benutze Wiremock für verspotten Rest Service für Junit im Frühjahr Boot-Anwendung. Mein Problem ist, dass ich nicht mehrere Match-Muster zuordnen kann.Wie genau Json in Wiremock für JUNIT

Junit.java

StringValuePattern pattern = WireMock.matching(".*"); 
givenThat(post(urlEqualTo("/softwares")) 
    .withHeader("Content-Type", equalTo("application/json")) 
    //TODO: Matching exact JSON body 
    .withRequestBody(pattern) 
    .willReturn(aResponse() 
    .withStatus(201) 
    .withHeader("Content-Type", "application/json") 
    .withBody("{\"userId\":\"ID009\"}"))); 

Above Code-Schnipsel, ich bin mit (. *) Für Spiel. Es funktioniert gut. Aber, ich brauche genaue Dinge anzupassen, die in wiremock des Mapping-Ordner in der entfalteten JSON ist

"matchesJsonPath" : "$.name", 
"matchesJsonPath" : "$.protocol", 
"matchesJsonPath" : "$.website", 
"matchesJsonPath" : "$.website.user", 
"matchesJsonPath" : "$.website.urlpath", 
"matchesJsonPath" : "$.website.userlist", 
"matchesJsonPath" : "$.website.resources.friendsList" 

Probe Json-die in wiremock des Mapping-Ordner bereitgestellt wird

{ 
    "request" : { 
    "urlPath" : "/softwares", 
    "method" : "POST", 
    "basicAuthCredentials" : {"username" : "username", "password" : "password"}, 
    "headers" : { 
     "Content-Type" : { 
     "equalTo" : "application/json" 
     } 
    }, 
    "bodyPatterns" : [ { 
     "matchesJsonPath" : "$.name", 
     "matchesJsonPath" : "$.protocol", 
     "matchesJsonPath" : "$.website", 
     "matchesJsonPath" : "$.website.user", 
     "matchesJsonPath" : "$.website.urlpath", 
     "matchesJsonPath" : "$.website.userlist", 
     "matchesJsonPath" : "$.website.resources.friendsList" 
    } ] 
    }, 

    "response" : { 
    "status" : 201, 
    "headers" : { "Content-Type" : "application/json"}, 
    "body": "{ \"userId\" : \"ID009\"}" 
    } 
} 

Antwort

0

Sie sollten extending wiremock versuchen.

Immer wenn eine Anfrage kommt, wird die Anfrage an eine Methode weitergeleitet. In dieser Methode können Sie die Anfragezeichenfolge erhalten und dann die gesamte Validierung durchführen und die Anfrage zurückgeben/umleiten/abweisen. Es ist eine sehr leistungsfähige Funktionalität.

hier sind einige weitere Beispiele http://one.wiremock.org/extending-wiremock.html