Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@SpringBootTest(randomPort = true) does not generate random port for actuator #4424

Closed
matsev opened this issue Nov 10, 2015 · 15 comments
Closed
Assignees
Labels
theme: testing Issues related to testing type: enhancement A general enhancement
Milestone

Comments

@matsev
Copy link
Contributor

matsev commented Nov 10, 2015

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

@philwebb
Copy link
Member

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 philwebb added the status: invalid An issue that we don't feel is valid label Nov 10, 2015
@matsev
Copy link
Contributor Author

matsev commented Nov 10, 2015

@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.

@philwebb
Copy link
Member

@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 philwebb reopened this Nov 10, 2015
@philwebb philwebb added type: enhancement A general enhancement status: ideal-for-contribution An issue that a contributor can help us with and removed status: invalid An issue that we don't feel is valid labels Nov 10, 2015
@matsev
Copy link
Contributor Author

matsev commented Nov 10, 2015

@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).

@FelipeAdorno
Copy link

FelipeAdorno commented Sep 13, 2016

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

@snicoll
Copy link
Member

snicoll commented Sep 13, 2016

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

@philwebb philwebb changed the title @WebIntegrationTest(randomPort = true) does not generate random port for actuator @SpringBootTest(randomPort = true) does not generate random port for actuator Dec 12, 2016
@philwebb
Copy link
Member

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

eddumelendez added a commit to eddumelendez/spring-boot that referenced this issue Jan 19, 2017
Previous to this commit, in order to provide a random port for
management key property should be explicitly provided. Now,
SpringBootTest annotation can do this if
WebEnvironment.RANDOM_SERVER_AND_MANAGEMENT_PORT is set.

```
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_SERVER_AND_MANAGEMENT_PORT)
public classSpringBootApplicationTests {

}
```

See spring-projectsgh-4424
@philwebb
Copy link
Member

Closing in favor of PR #8023

@philwebb philwebb added status: duplicate A duplicate of another issue and removed status: ideal-for-contribution An issue that a contributor can help us with labels Jan 19, 2017
@philwebb philwebb reopened this Feb 1, 2017
@philwebb
Copy link
Member

philwebb commented Feb 1, 2017

Reopening due to comment #8023 (comment)

@guenhter
Copy link

@philwebb is there any way to do this now?

@wilkinsona wilkinsona removed the status: duplicate A duplicate of another issue label May 14, 2017
@philwebb
Copy link
Member

@guenhter The issue is still open

@alla-gofman
Copy link

Is there any news with this fix?

@philwebb philwebb added this to the Backlog milestone Mar 21, 2018
@philwebb philwebb added the theme: testing Issues related to testing label Mar 21, 2018
@mbhave mbhave self-assigned this Sep 6, 2018
@mbhave mbhave added the for: team-attention An issue we'd like other members of the team to review label Sep 7, 2018
@radistao
Copy link

radistao commented Sep 11, 2018

So, are there only these 2 workarounds:

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

Correct?

@mbhave mbhave removed the for: team-attention An issue we'd like other members of the team to review label Sep 14, 2018
@mbhave mbhave modified the milestones: 2.1.x, 2.1.0.M4 Sep 14, 2018
@mbhave mbhave closed this as completed in d5a197f Sep 14, 2018
@radistao

This comment has been minimized.

@philwebb

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: testing Issues related to testing type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

9 participants