Skip to content

@LocalServerPort fails to inject port when using inheritance and @Nested test classes #35894

@fraj

Description

@fraj

Versions

Version of Spring Boot: 4.0.0, after migration from 3.5.x

Context

I have a JUnit test class annotated with @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT). This class does not contain any tests itself. Instead, it inherits from an abstract class that implements test methods within an inner classes annotated with JUnit's @Nested. In the abstract parent class, I want to inject the value of the web server's port using @LocalServerPort on a local field.

Observed

A 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 behavior only occurs when I use both class inheritance and a 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 and nested test class.

Attachments

Simplified project to reproduce the issue:

issue-spring-boot-4.zip

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: duplicateA duplicate of another issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions