'JsonMappingException when trying to transform a stubMapping to use regex urlPattern instead of standard url using Wiremock Java Library
I'm trying to transform a stubMapping to detect the request using a regex, instead of using simple string equality. But When I do this, a NPE is thrown when the Json conversion is processed. When I debug the list of stubMappings which is converted when the NPE appears, the NPE is thrown when I try to get the element in the list which contains the stubMapping with the regex urlPattern, yet my transformer returns a stubmapping which is not null.
Here is the Exception:
Exception in thread "Timer-8" com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.google.common.collect.Lists$TransformingRandomAccessList[2])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:397)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:368)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:338)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:123)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1516)
at com.fasterxml.jackson.databind.ObjectWriter._writeValueAndClose(ObjectWriter.java:1217)
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:1086)
at com.github.tomakehurst.wiremock.common.Json.write(Json.java:88)
at com.github.tomakehurst.wiremock.common.Json.write(Json.java:74)
at com.github.tomakehurst.wiremock.recording.Recorder.stopRecording(Recorder.java:92)
at com.github.tomakehurst.wiremock.core.WireMockApp.stopRecording(WireMockApp.java:486)
at com.github.tomakehurst.wiremock.WireMockServer.stopRecording(WireMockServer.java:470)
And what I'm doing in my stub mapping transformer class:
@Override
public StubMapping transform(StubMapping stubMapping, FileSource fileSource, Parameters parameters) {
RequestPattern originalRequest = stubMapping.getRequest();
String urlPattern = originalRequest.getUrl();
urlPattern = urlPattern.replaceAll("ownerName=(.*)&", Matcher.quoteReplacement("ownerName=(.*)&"));
RequestPattern request = new RequestPattern(
originalRequest.getScheme(),
originalRequest.getHost(),
originalRequest.getPort(),
WireMock.urlMatching(urlPattern),
originalRequest.getMethod(),
originalRequest.getHeaders(),
originalRequest.getQueryParameters(),
originalRequest.getCookies(),
originalRequest.getBasicAuthCredentials(),
originalRequest.getBodyPatterns(),
originalRequest.getCustomMatcher(),
originalRequest.hasInlineCustomMatcher() ? originalRequest.getMatcher() : null,
originalRequest.getMultipartPatterns()
);
stubMapping.setRequest(request);
return stubMapping;
}
Can someone help me to solve this? Am I doing something wrong ? I didn't find out how to do it with the RequestPatternBuilder, so I did it directly with the RequestPattern constructor.
And the problem appears only when I use WireMock.urlMatching(urlPattern) for the url. When I replace it by WireMock.urlEqualTo(urlPattern) in my code, the NPE is not thrown anymore.
Thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
