【问题标题】:Getting I/O error while executing JUnit test case for Spring Controller为 Spring Controller 执行 JUnit 测试用例时出现 I/O 错误
【发布时间】: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


【解决方案1】:

如果您已正确配置测试环境以运行MockRestServiceServer

(我的意思是@RunWith(SpringRunner.class)@RestClientTest(ClassUnderTestThatCallsTheMockServer.class)),请确保您没有使用= new MockServer() 实例化您的模拟服务器,而只需使用来自spring 上下文的@Autowired 实例(因为实例是开箱即用的)。

我看到您的测试中有很多继承和覆盖的方法,使用this.returnSomething... 调用事物,因此请确保您没有在 spring 上下文之外实例化事物。

这是一个获取一些帖子的模拟服务器的简单示例:

    @RunWith(SpringRunner.class)
    @RestClientTest(PostClient.class)
    public class PostClientMockTest {

        // class under test
        @Autowired
        private PostClient postClient;

        // autowired mock server from the spring context
        @Autowired
        private MockRestServiceServer mockRestServiceServer;

        @Test
        public void readPosts() throws Exception {

            String mockJsonResponse = "My response";

            mockRestServiceServer.expect(requestTo("https://myurl.com/posts?userId=1"))
                    .andRespond(withSuccess(mockJsonResponse, MediaType.APPLICATION_JSON_UTF8));

            List<Post> posts = postClient.readPosts(1);
            assertEquals(9, posts.size());
            mockRestServiceServer.verify();
        }

    }

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多