Skip to content

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

Closed
@sbrannen

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"

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