url中的java MockRestServiceServer转义字符

url中的java MockRestServiceServer转义字符,java,unit-testing,mockito,Java,Unit Testing,Mockito,在我们的项目中,我们使用带有非常不寻常的RESTAPI的外部系统。 url中包含方括号: api/v1/series?match[]=up 现在我们要测试我们自己的RESTAPI,并模拟这个外部系统的响应。 所以我们在单元测试中使用MockRestServiceServer对象 mockServer = MockRestServiceServer.createServer(restTemplate); mockServer.expect(ExpectedCount.manyTi

在我们的项目中,我们使用带有非常不寻常的RESTAPI的外部系统。 url中包含方括号:

api/v1/series?match[]=up
现在我们要测试我们自己的RESTAPI,并模拟这个外部系统的响应。 所以我们在单元测试中使用MockRestServiceServer对象

    mockServer = MockRestServiceServer.createServer(restTemplate);
    mockServer.expect(ExpectedCount.manyTimes(), requestTo(UriComponentsBuilder.fromHttpUrl(prometheusURL + "series?match[]=up")
            .build().toUri()))
            .andExpect(method(HttpMethod.GET))
            .andRespond(withSuccess(promResponseMetricUpInfo, MediaType.APPLICATION_JSON));
在我们的服务内部,我们将这个外部API称为

restTemplate.getForObject(prometheusURL + "series?match[]=up", ResponseObjectForSeries.class);
但结果是我们有以下错误:

java.lang.AssertionError: Unexpected request expected:<http://localhost:9090/api/v1/series?match[]=up> but was:<http://localhost:9090/api/v1/series?match%5B%5D=up>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.match.MockRestRequestMatchers$5.match(MockRestRequestMatchers.java:121) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.DefaultRequestExpectation.match(DefaultRequestExpectation.java:84) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.SimpleRequestExpectationManager.validateRequestInternal(SimpleRequestExpectationManager.java:55) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.AbstractRequestExpectationManager.validateRequest(AbstractRequestExpectationManager.java:75) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.web.client.MockRestServiceServer$MockClientHttpRequestFactory$1.executeInternal(MockRestServiceServer.java:289) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.mock.http.client.MockClientHttpRequest.execute(MockClientHttpRequest.java:94) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:652) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
java.lang.AssertionError:应为意外请求:但为:
在org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
在org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
在org.springframework.test.web.client.match.MockRestRequestMatchers$5.match(MockRestRequestMatchers.java:121)~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
在org.springframework.test.web.client.defaultRequestExpection.match(defaultRequestExpection.java:84)~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
在org.springframework.test.web.client.SimpleRequestExpectationManager.validateRequestInternal(SimpleRequestExpectationManager.java:55)~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
在org.springframework.test.web.client.AbstractRequestExpectationManager.validateRequest(AbstractRequestExpectationManager.java:75)~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
在org.springframework.test.web.client.MockRestServiceServer$mockclienthtprequestfactory$1.executeInternal(MockRestServiceServer.java:289)~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
在org.springframework.mock.http.client.MockClientHttpRequest.execute(MockClientHttpRequest.java:94)~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
在org.springframework.web.client.restemplate.doExecute(restemplate.java:652)~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
我们怎样才能摆脱这些方括号呢?
我尝试在测试中使用反斜杠或%5B%5D明确性。但是它没有帮助。

使用

URLEncoder.encode()

在测试中

使用

URLEncoder.encode()

在测试中

如何明确尝试%5B%5D?您介意与我们共享代码吗?我的意思是我刚刚编写了mockServer.expect(ExpectedCount.manyTimes()、requestTo(UriComponentsBuilder.fromHttpUrl(PrometheSuUrl+“series?match%5B%5D=up”)。但是这些符号也进行了编码。因此,如果删除toUri(),方括号将被编码两次它能工作吗?你如何明确地尝试%5B%5D?你介意与我们共享代码吗?我的意思是我刚刚编写了mockServer.expect(ExpectedCount.manyTimes()、requestTo(UriComponentsBuilder.fromHttpUrl(PrometheSurl+“series?匹配%5B%5D=up))。但是这些符号也被编码了。因此,如果你删除toUri(),方括号就被编码了两次,这样行吗?