I am having problem with my integration test. I am using springboot(kotlin) + spock. When I run my application, it loads up normally but when I run integration test, it fails with these exceptions:
I/O error on GET request for "http://localhost:0/events/1": Cannot assign requested address: connect
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:0/events/1": Cannot assign requested address: connect
and:
Caused by: java.net.BindException: Cannot assign requested address: connect
The code of integration test and application.properties
@RunWith(SpringRunner)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class EventControllerSpec extends Specification {
TestRestTemplate restTemplate = new TestRestTemplate()
@LocalServerPort
private int port
def 'should return 200'(){
when:
ResponseEntity<Event> response = restTemplate.getForEntity("http://localhost:$port/events/1", Event.class)
then:
response.getStatusCode().is2xxSuccessful()
}
}
server.port=9000
spring.profiles.active=test
spring.application.name=eventplanner
#and the rest of the properties connected with db
I know that these exceptions are falling due to lack of ports, but why? I have tried almost evceryting
I tried tip to set port
to preseted value, used property webEnviroment.DefinedPort
, tried code without any params in @springBootTest
. I even restored my firewall settings to default.
What should I do? I know that this is problem with config, but I am out of ideas what may help.
Maybe I should change something in gradle file etc?
#EDIT I also checked my tcp/ip config and everything is fine, it's just problem with test