【发布时间】:2019-01-29 10:28:26
【问题描述】:
我正在执行一个 JUnit 测试用例来测试服务。我在运行测试用例时面临 java.lang.AssertionError 。下面给出的是测试类和 url builder 代码。
@Test
public void testSuccess() throws Exception
{
final String mockedResponseJson = rawJsonFromFile("com/cnanational/preferences/client/rule-sets/getRuleSetsResponse.json");
MockRestServiceServer mockServer = mockServer();
mockServer.expect(requestTo(dummyUri()))
.andExpect(queryParam("ruleSetDescription", RULE_DESCRIPTION))
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(
mockedResponseJson,
MediaType.APPLICATION_JSON));
ServiceClientResponse<GetRuleSetsResponse> response = executeDummyRequest();
mockServer.verify();
assertThat(response.isSuccessful(), equalTo(true));
}
实际要测试的服务代码如下:
URI targetUri = UriComponentsBuilder.fromUri(this.preferencesServiceUri).path(this.rulesSetsPath)
.queryParam("ruleSetDescription", params).build()
.toUri();
下面是我得到的断言错误的堆栈跟踪:
java.lang.AssertionError: Unexpected request expected:<http://localhost:8039> but was:<http://localhost:8039?ruleSetDescription=TestRuleDescription>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
at org.springframework.test.web.client.match.MockRestRequestMatchers$5.match(MockRestRequestMatchers.java:121)
具有类似 JUnit 案例和类似 GET 端点的其他测试类工作正常。我错过了什么吗?请给我一些建议。
【问题讨论】:
-
您的
response似乎失败了。您能否发布AssertionError完整的堆栈跟踪? -
@gokareless 我已经发布了 AssertionError 堆栈跟踪。
标签: rest junit resttemplate spring-rest mockserver