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

TestRestTemplate HttpHostConnectException: Connect to localhost:8082 [localhost/127.0.0.1] failed: Connection refused

我只是从 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.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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.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

奇怪的是,教程示例无法正常运行。

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