Spring Boot version: 3.2.3 JUnit version: 5.10.2 Kotlin version: 1.9.22
The project is based on Spring Integration and uses integration flows.
While executing tests annotated with @SpringBootTest that execute an integration flow, some of these tests sporadically fail with a stacktrace that resembles the following one:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dc29da35-08dc-4ed7-ae2e-80882f2466a9.channel#7' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:895)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:82)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:73)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.afterTestMethod(ResetMocksTestExecutionListener.java:67)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:487)
at org.springframework.test.context.junit.jupiter.SpringExtension.afterEach(SpringExtension.java:278)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1845)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:276)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
at java.util.ArrayList.forEach(ArrayList.java:1511)
at java.util.ArrayList.forEach(ArrayList.java:1511)
at java.util.ArrayList.forEach(ArrayList.java:1511)
Names of the beans that can't be found vary, but it seems like they are always the ones managed by Spring Integration - including channel, subFlow, or sometimes even just an UID (like dc29da35-08dc-4ed7-ae2e-80882f2466a9).
It seems that sometimes these beans get removed from the context after the ResetMocksTestExecutionListener.resetMocks() has read their names with String[] names = beanFactory.getBeanDefinitionNames(); - as a consequence their definitions can't be found and an exception is thrown.
There is a catch (NoSuchBeanDefinitionException ex) present in that resetMocks method, but it doesn't cover the phase of iterating through the bean names and their definitions. Perhaps it should?
The application itself works correctly and never runs into a missing bean, this issue is strictly test related and the only workaround appears to be an exclusion of ResetMocksTestExecutionListener by using @TestExecutionListeners, which is not an ideal situation.
Comment From: mhalbritter
Thanks for the report. Weird that the beans vanish between beanFactory.getBeanDefinitionNames() and beanFactory.getBeanDefinition(name).
I agree that catch NoSuchBeanDefinitionException would help, but maybe there's a different problem the root cause. Let me investigate.
Comment From: mhalbritter
Do you happen to have a small reproducer which can reproduce this problem, even if it's only sometimes?
Comment From: Loreth
I really wish I could provide that, but it's a complex commercial service and I'm not at a liberty of sharing the code, at least not without spending a significant amount of time to minimize and anonymize it.
Comment From: wilkinsona
Perhaps you could start with an app created using https://start.spring.io and then add the bare minimum to it to reproduce the problem?
Comment From: artembilan
@Loreth ,
Can you probably just share with us a unit test where you use those mock beans and IntegrationFlow ?
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.