【发布时间】:2018-08-20 19:50:41
【问题描述】:
我正在执行一个测试用例来调用 spring 控制器(GET 方法)。但是,它会抛出 I/O 错误。
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8039": Connect to localhost:8039 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8039 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:674)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:636)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
下面是我正在执行的抛出上述错误的测试用例类。
public class GetRuleSetsTests extends PreferencesAdminClientTestApplicationTests<GetRuleSetsResponse>{
@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(method(HttpMethod.GET))
.andExpect(queryParam("ruleSetDescription", "TestRuleDescription"))
.andRespond(withSuccess(
mockedResponseJson,
MediaType.APPLICATION_JSON));
ServiceClientResponse<GetRuleSetsResponse> response = executeDummyRequest();
mockServer.verify();
assertThat(response.isSuccessful(), equalTo(true));
GetRuleSetsResponse programResponse = response.getParsedResponseObject();
assertThat(programResponse.getRuleSets().size(), equalTo(2));
}
@Override
public URI dummyUri() {
return UriComponentsBuilder.fromUri(baseUri())
.path(this.endpointProperties.getRuleSets())
.build()
.toUri();
}
}
我错过了什么?任何意见表示赞赏。
【问题讨论】:
-
@RunWith(SpringRunner.class)和@RestClientTest在您的班级上,以便配置MockRestServiceServer -
@UroshT。我在父类中已经有了那个注释,但错误仍然来了
-
是
@RestClientTest(YourClassThatCallsTheMockServer.class)吗? -
请告诉我们
executeDummyRequest() -
@Urosh T protected RestTemplate restTemplate() { return this.restTemplateProvider.getRulesRestTemplate(); } public ServiceClientResponse
executeDummyRequest() { return this.preferencesClientService.getRulesSets("TestRuleDescription", TEST_USER_NAME, TEST_CORRELATION_ID, TEST_REQUESTOR_APP); } 公共 URI dummyUri() { return UriComponentsBuilder.fromUri(baseUri()) .path(this.endpointProperties.getRuleSets()) .build() .toUri(); }
标签: spring rest junit controller ioerror