Using WebEnvironment.RANDOM_PORT the actual port value available during test execution; in case test property reffer to this variable [1] then value is 0. As workaround custom context initializer used [2] but its not convenient.

  1. https://github.com/amarkevich/spring-boot-jwt-test/blob/master/spring-boot-application/src/test/java/com/wedextim/spring/boot/application/ApplicationTest.java#L42
  2. https://github.com/amarkevich/spring-boot-jwt-test/blob/master/spring-boot-jwt-test/src/main/java/com/wedextim/spring/boot/test/ServerPortInitializer.java

Comment From: mbhave

We discussed this on the team call and it doesn't look like something we can easily support. WebEnvironment.RANDOM_PORT causes server.port to be set to 0 which is a signal for the OS to allocate an ephemeral port. We've seen that findAvailableTcpPort doesn't reliably return an available port and can cause flakey test.

For your case, it seems like you need to use the server.port to setup the jwk-set-uri property. You can take a look at this Spring Security sample to see how they've set up a mock authorization server for use in tests.