TestRestTemplate HttpHostConnectException:连接到本地主机:8082 [localhost / 127.0.0.1]失败:连接被拒绝

时间:2020-08-18 10:12:59

标签: java spring spring-boot resttemplate spring-resttemplate

我只是从https://github.com/eugenp/tutorials/tree/master/spring-resttemplate克隆源代码,然后 无法在本地执行测试givenFooService_whenCallHeadForHeaders_thenReceiveAllHeaders()

我收到了下一个例外

org.springframework.web.client.ResourceAccessException: I/O error on HEAD request for "http://localhost:8082/spring-rest/foos": Connect to localhost:8082 [localhost/127.0.0.1] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8082 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:748)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
    at org.springframework.web.client.RestTemplate.headForHeaders(RestTemplate.java:366)
    at org.springframework.boot.test.web.client.TestRestTemplate.headForHeaders(TestRestTemplate.java:279)
    at com.baeldung.web.test.TestRestTemplateBasicLiveTest.givenFooService_whenCallHeadForHeaders_thenReceiveAllHeaders(TestRestTemplateBasicLiveTest.java:98)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8082 [localhost/127.0.0.1] failed: Connection refused (Connection refused)

有关存储库https://www.baeldung.com/spring-boot-testresttemplate的其他信息

奇怪的是,教程示例无法正常工作。

1 个答案:

答案 0 :(得分:1)

这些是集成测试。在执行测试之前,您需要启动spring boot应用程序。主班-RestTemplateConfigurationApplication