spring-framework
spring-framework copied to clipboard
@LocalServerPort fails to inject port value in JUnit tests when using inheritance + nested test classes
Version of Spring Boot : 4.0.0, after migration from 3.5.x
Context: I have some JUnit test class annotated with @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT). This class does not contain any test case in itself. Instead, it inherits an abstract class that implements test methods. These test methods are put in inner classes annotated with JUnit's @Nested. In the abstract class, I want to inject the value of the web server's port using @LocalServerPort.
Observed: An NumberFormatException occurs when I try to retrieve the port value as an int. When I retrieve it as a String, I obtain "${local.server.port}". This behaviour only occurs when I use both class inheritance + nested test class in the parent class. With nested test classes directly inside the @SpringBootTest class, the port value is OK. With test methods in an abstract parent class but without a nested class, the port value is OK. The configuration with both inheritance and nested test classes works with Spring Boot 3.5.
Expected: Have the port value correctly injected in the configuration with inheritance + nested test class
Attached : Simplified project to reproduce the issue