Skip to content

Bean Override in superclass takes precedence over Bean Override in subclassĀ #34194

Closed

Description

@sbrannen

Overview

While working on #33925, I noticed that the search algorithm for @BeanOverride-annotated fields registers bean override handlers with "bottom up" semantics; whereas, handlers should be registered with "top down" semantics while traversing a test class hierarchy.

In other words, a bean override in a superclass currently takes precedence over a bean override in a subclass, which is incorrect.

Example

@ExtendWith(SpringExtension.class)
class BaseTests {

	@TestBean("enigma")
	String enigma;

	static String enigma() {
		return "enigma in superclass";
	}

	@Test
	void test() {
		assertThat(enigma).isEqualTo("enigma in superclass");
	}
}
class ExtendedTests extends BaseTests {

	@TestBean(name = "enigma", methodName = "localEnigma")
	String enigma;

	static String localEnigma() {
		return "enigma in subclass";
	}

	@Test
	@Override
	void test() {
		assertThat(enigma).isEqualTo("enigma in subclass");
	}
}

BaseTests passes as expected; however, ExtendedTests fails with the following error.

org.opentest4j.AssertionFailedError: 
expected: "enigma in subclass"
 but was: "enigma in superclass"

Activity

  1. added this to the 6.2.2 milestone on Jan 4, 2025
  2. self-assigned this
    on Jan 4, 2025
  3. added a commit that references this issue on Jan 5, 2025
    ef4f1f0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: testIssues in the test moduletype: bugA general bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions