Skip to content

Duplicate BeanOverrideHandler discovered in @Nested test case with superclass from different class or in interface implemented multiple times #34844

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

Closed
aykborstelmann opened this issue Apr 30, 2025 · 4 comments
Assignees
Labels
in: test Issues in the test module type: regression A bug that is also a regression
Milestone

Comments

@aykborstelmann
Copy link

aykborstelmann commented Apr 30, 2025

Consider the following extension of the example given in #34324 :

@SpringBootTest
class SomeTest extends AbstractTest {
  @Nested
  class SomeNestedTest extends AbstractTest.BaseClassForNestedTests {}
}

abstract class AbstractTest {

  @MockitoBean SomeService someService;

  abstract class BaseClassForNestedTests {

    @Test
    void test() {}
  }
}

@Service
class SomeService {}

Upgrading from Spring Boot 3.4.1 to 3.4.3 thus upgrading Spring Framework from 6.2.1 to 6.2.3 (skipping 3.4.2 and 6.2.2 because of #34324) again leads to the same stacktrace.

java.lang.IllegalStateException: Duplicate BeanOverrideHandler discovered in test class SomeTest$SomeNestedTest: [MockitoBeanOverrideHandler@12ed9db6 field = SomeService AbstractTest.someService, beanType = SomeService, beanName = [null], strategy = REPLACE_OR_CREATE, reset = AFTER, extraInterfaces = set[[empty]], answers = RETURNS_DEFAULTS, serializable = false]
	at org.springframework.util.Assert.state(Assert.java:101)
	at org.springframework.test.context.bean.override.BeanOverrideContextCustomizerFactory.lambda$findBeanOverrideHandlers$1(BeanOverrideContextCustomizerFactory.java:55)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.springframework.test.context.bean.override.BeanOverrideContextCustomizerFactory.findBeanOverrideHandlers(BeanOverrideContextCustomizerFactory.java:54)
	at org.springframework.test.context.bean.override.BeanOverrideContextCustomizerFactory.createContextCustomizer(BeanOverrideContextCustomizerFactory.java:46)
	at org.springframework.test.context.bean.override.BeanOverrideContextCustomizerFactory.createContextCustomizer(BeanOverrideContextCustomizerFactory.java:38)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.getContextCustomizers(AbstractTestContextBootstrapper.java:360)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:332)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:267)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:215)
	at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
	at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:111)
	at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:142)
	at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:126)
	at org.springframework.test.context.junit.jupiter.SpringExtension.getTestContextManager(SpringExtension.java:363)
	at org.springframework.test.context.junit.jupiter.SpringExtension.beforeAll(SpringExtension.java:128)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 30, 2025
@sbrannen sbrannen self-assigned this May 1, 2025
@sbrannen sbrannen added the in: test Issues in the test module label May 1, 2025
@sbrannen sbrannen added this to the 6.2.7 milestone May 1, 2025
@sbrannen sbrannen added type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 1, 2025
@sbrannen
Copy link
Member

sbrannen commented May 1, 2025

@aykborstelmann, thanks for reporting the issue and providing the reproducer.

I have confirmed that this is also a regression.

While looking into this, I also noticed that a duplicate is also reported for @MockitoBean declared at the type level on an interface that is implemented at multiple levels in a type hierarchy or enclosing class hierarchy.

In my fix in 305686d I should have actually tracked all visited types, not just enclosing classes.

So, I'll address the issue you've reported as well as the issue for interfaces in the same fix.

@sbrannen sbrannen changed the title Duplicate BeanOverrideHandler discovered in @Nested test case with super class from different class Duplicate BeanOverrideHandler discovered in @Nested test case with superclass from different class or in interface implemented multiple times May 1, 2025
@aykborstelmann
Copy link
Author

aykborstelmann commented May 2, 2025

Thank you for your fast response and confirming the reproducer !

@sbrannen
Copy link
Member

sbrannen commented May 2, 2025

This has been fixed in e8f873a for inclusion in the upcoming 6.2.7 release.

Feel free to try it out in 6.2.7 snapshots, and let us know if the issue is resolved for you.

@aykborstelmann
Copy link
Author

Thank you very much for the fast fix. I can confirm that it works 👍🏻

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: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

3 participants