0

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

  • It looks "port" config value "0" is not correctly initialized. So it is NOT about network, It is about your configuration. You could check this thread https://stackoverflow.com/questions/50239369/spring-boot-test-failing-to-autowire-port-with-localserverport-annotation Hope it helps. – Stone Apr 05 '23 at 12:41
  • Thanks for sending me thread, I tried to implemented all suggested changes, but unfortunately none of them worked for me. – Jakub Kołacz Apr 05 '23 at 13:59
  • What version of Spring and Spock are you using? – Leonard Brünings Apr 05 '23 at 18:22
  • Spring is 3.0.5 and spock 2.3-groovy-4.0 – Jakub Kołacz Apr 06 '23 at 00:00
  • For Spring Boot 3, you want to upgrade to [Spock 2.4-M1](https://spockframework.org/spock/docs/2.4-M1/all_in_one.html#_2_4_m1_2022_11_30). – kriegaex Apr 06 '23 at 08:36
  • I changed spock version, and it still doesnt work – Jakub Kołacz Apr 06 '23 at 23:02
  • Fine, then please learn [how to ask a question on SO](https://stackoverflow.com/help/how-to-ask) and provide a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). Thank you. I am sure, you will get good answers quickly then. A link to a GitHub repo would be nice. – kriegaex Apr 07 '23 at 11:31

0 Answers0