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

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

Closed
sbrannen opened this issue Jan 4, 2025 · 0 comments
Closed
Assignees
Labels
in: test Issues in the test module type: bug A general bug
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Jan 4, 2025

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"
@sbrannen sbrannen added in: test Issues in the test module type: bug A general bug labels Jan 4, 2025
@sbrannen sbrannen added this to the 6.2.2 milestone Jan 4, 2025
@sbrannen sbrannen self-assigned this Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: bug A general bug
Projects
None yet
Development

No branches or pull requests

1 participant