Spring-boot: @SpringBootTest(randomPort = true) does not generate random port for actuator

Created on 10 Nov 2015  路  15Comments  路  Source: spring-projects/spring-boot

Step by step

  1. Add the actuator to a web project
  2. Change the port configuration of the actuator, e.g. management.port=8081
  3. Create an integration test that uses the @WebIntegrationTest(randomPort = true)
  4. Execute the test

    Expected result

The actuator endpoints should be assigned to a random port.

Actual result

The actuator endpoints are assigned to the same port as in the configuration file (i.e. 8081 in the example above)

Version

Spring Boot 1.3.0.RC1

testing enhancement

Most helpful comment

Is there any news with this fix?

All 15 comments

The randomPort attribute only affects the main server port, not the actuator (from the Javadoc):

Convenience attribute that can be used to set a server.port=0 Environment property which usually triggers listening on a random port.

We can't really use it to set a random management port since many tests will expect that the actuator is running on the same port as the main server.

You can probably achieve what you need by setting management.port=0 for your tests.

@philwebb The problem that I am currently facing is that I cannot have my application running and executing integration tests concurrently because the actuator port collisions when using randomPort. That said, you are correct, I can use the workaround that you suggested by using @WebIntegrationTest({"server.port=0", "management.port=0"}) and then autowire the values in the integration test by using @Value("${local.server.port}") and @Value("${local.management.port}") respectively.

Nevertheless, I can see an updated solution to the random port behavior where some logic checks server.port and management.port in the Environment. If just server.port has been set (or both have been set and have the same the value) then the random port can be used to generate one single value for both the application and the actuator (thus not affecting any tests that rely on having the same port value). However, if both properties have been specified then two different random values can be generated.

@matsev I'd have to take a look at the code but I have a feeling that the randomPort logic is run before the Environment is populated. I'm not sure it's possible for us to inspect for management.port properties. I guess the easiest fix might be to add a randomManagementPort attribute to the annotation.

@philwebb Ok, just a suggestion, not a big deal. If you need to create a second attribute I guess that it is of less value since the developer needs to key about as many characters anyway (although tab-completion will help you).

I can try help in this issue if not solved, ok @philwebb?

@FelipeAdorno no need to ask, the issue has ideal-for-contribution so we're waiting for your PR, thanks!

The updated @SpringBootTest annotation might make this easier to implement now. We could add WebEnvironment.RANDOM_SERVER_AND_MANAGEMENT_PORT

Closing in favor of PR #8023

@philwebb is there any way to do this now?

@guenhter The issue is still open

Is there any news with this fix?

So, are there only these 2 workarounds:

  • @DirtiesContext for tests with the same contexts
  • management.server.port: 0 for test profile

Correct?

shall it be reflected in the release notes? https://github.com/spring-projects/spring-boot/releases/tag/v2.1.0.M3
or M4 ?

@radistao I'll be in the M4 release notes, but we've not written them yet.

Was this page helpful?
0 / 5 - 0 ratings