我在使用Spring Boot和Spock框架进行集成测试时遇到问题。测试期间收到的错误"关于" http://localhost:8080/farm "的GET请求的I/O错误":连接被拒绝:"
我的猜测是应用程序在集成测试期间无法启动,但我不知道为什么。我已经尝试了多种方法来解决它,但我没有想法
我的控制器集成测试代码:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class FarmControllerTest extends Specification {
RestTemplate restTemplate = new RestTemplate()
def "integration test test"() {
when: "test"
def res = restTemplate.getForEntity("http://localhost:8080/farm", String.class)
then: "assert"
res.body == "implement getting farm for logged in user"
}
}
I/O error on GET request for "http://localhost:8080/farm": Connection refused: connect
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080/farm": Connection refused: connect
at app//org.springframework.web.client.RestTemplate.createResourceAccessException(RestTemplate.java:888)
at app//org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:868)
at app//org.springframework.web.client.RestTemplate.execute(RestTemplate.java:764)
at app//org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:405)
at com.example.farmsystem.controllers.FarmControllerTest.integration test test(FarmControllerTest.groovy:24)
Caused by: java.net.ConnectException: Connection refused: connect
at java.base/sun.nio.ch.Net.connect(Net.java:579)
at java.base/sun.nio.ch.Net.connect(Net.java:568)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.Socket.connect(Socket.java:633)
at java.base/java.net.Socket.connect(Socket.java:583)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:183)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:498)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:603)
at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:246)
at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:351)
at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:373)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1309)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1242)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1128)
at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1057)
at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:75)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:862)
... 3 more
1条答案
按热度按时间c3frrgcw1#
我已经找到了解决方案,基本上我所缺乏的是FarmControllerTest类上的
@ContextConfiguration(classes = FarmSystemApplication.class)
注解,正如我在评论中所述,问题是应用程序甚至没有在集成测试之前启动。