0

I am testing a Spring Boot application with Spock, but on one of the test cases I need to mock or stub the calls to the auth server (using oauth 2) so I'm trying to redirect the requests to a dummy server for testing and make the methods return a fixed token. However, i overwrite the port at runtime but i get an error because the dummy server is on a fixed port (read from the application-test.yml), is there a way to change this at runtime to make the server match the random port that the test is running on? this is my setup function:

`def setup() {
        omcService.soapClient = Stub(SOAPClient)
        String url = "http://localhost:${port}"
        nonRetryableExceptionProcessor.omsUrl = url
        omsService.omsUrl = url
        omsService.authUrl = "$url/oauth/token?scope=all"
        omsService = Spy(OmsService)
        producerTemplate.start()
    }

When I debug this test, the properties are changed but when the application performs a GET operation, it points to localhost:4321 always, which is not the random port picked up by Spring

1 Answers1

1

You can inject random port into your test.

For example using @LocalManagementPort:

    @LocalManagementPort
    int port;

Or directly using @Value:

    @Value("${local.server.port}")
    int port;

But if above doesn't work, then I believe this is your last resort:

    int port = context.embeddedServletContainer.port

Having it injected, you can perform GET to the server on that port.

Oleksii Zghurskyi
  • 3,967
  • 1
  • 16
  • 17
  • I have done that, in fact the `${port}` variable is set up in that way, the problem is, for some reason, the application test starts on that random port, and I inject that random port value this way, but when the simulated GET gets called, it points to the port in the propety file, not the port that the app is running on – Juan Jose Villalobos Feb 08 '19 at 20:07
  • 1
    Let's try to use `${randomServerPort}` instead of `${port}`, where randomServerPort name of variable with injected port. – Oleksii Zghurskyi Feb 08 '19 at 20:19
  • Need full test setup to understand why it's not working. But we can try another approach: let's start on predefined port by setting `webEnvironment = WebEnvironment.DEFINED_PORT`. Then `port` from your application.yml will be used everywhere. – Oleksii Zghurskyi Feb 08 '19 at 20:45
  • Ok, I think this can help `int port = context.embeddedServletContainer.port` instead of `@Value("${local.server.port}")` or `@LocalManagementPort`. – Oleksii Zghurskyi Feb 08 '19 at 20:54
  • It seems similar question was already asked before [here](https://stackoverflow.com/questions/24643483/how-to-find-port-of-spring-boot-container-when-running-a-spock-test-using-proper?rq=1) and [here](https://stackoverflow.com/questions/24405727/integration-test-with-spring-boot-and-spock/24412145#24412145) – Oleksii Zghurskyi Feb 08 '19 at 21:06
  • Found the error, was a newbie mistake by me, I was setting the properties in an `@Autowired` bean that was not the object that was used in runtime (another intance was created inside another object, so it was a mixup on my part, thanks for the help! – Juan Jose Villalobos Feb 11 '19 at 20:54