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

huangapple go评论118阅读模式
英文:

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: 在 HEAD 请求“http://localhost:8082/spring-rest/foos”时出现 I/O 错误:无法连接到 localhost:8082 [localhost/127.0.0.1]:连接被拒绝(Connection refused);嵌套异常是 org.apache.http.conn.HttpHostConnectException:无法连接到 localhost:8082 [localhost/127.0.0.1]:连接被拒绝(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: 无法连接到 localhost:8082 [localhost/127.0.0.1]:连接被拒绝(Connection refused)

关于仓库 https://www.baeldung.com/spring-boot-testresttemplate 的附加信息:

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

英文:

I just clone source code from https://github.com/eugenp/tutorials/tree/master/spring-resttemplate and
can't execute test givenFooService_whenCallHeadForHeaders_thenReceiveAllHeaders() locally.

I received next exception

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)

Additional info about repository https://www.baeldung.com/spring-boot-testresttemplate

It's strange that tutorials examples not works properly.

答案1

得分: 1

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

英文:

These are integration tests. You need to start the spring boot app before executing the tests. Main class - RestTemplateConfigurationApplication

huangapple
  • 本文由 发表于 2020年8月18日 18:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63466480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定