-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
ApplicationContext
reloaded when @MockitoBean
and @ContextHierarchy
are mixed on single class
#33293
Comments
Thanks for the sample. This is behaving as I would expect given how the test framework, The test framework creates a
As the mock bean definitions of I'm not sure there's much that we can do to improve the situation here. Perhaps we could allow |
Hi, if it's not possible to automatically determine which contexts in the hierarchy have which mockbeans, then perhaps adding a parameter to @MockBean to manually specify the context name is a good alternative ? Also, maybe the technique of using a dummy inheritance structure to group the mockbeans definitions with the context they are associated with can be documented ? Although it won't work when your class needs to subclass another class.. In which case the (automatic or manual) association of mockbeans to their context in the hieararchy seems like the only way to solve the problem ? Cheers, |
Well, So, if Does that meet your needs? |
Thanks, Sam. Sorry that it has taken so long to get back to this. It looks like I'll flag this one so that we can decide if this is something that we want to do, or if we're happy with the current situation where test-class inheritance can be used to provide the desired separation. |
I've moved this issue to framework given the support for mock bean has been moved to framework. @sbrannen perhaps we could do something a bit more direct now the support is in |
@MockitoBean
and @ContextHierarchy
are mixed on single class
@MockitoBean
and @ContextHierarchy
are mixed on single classApplicationContext
reloaded when @MockitoBean
and @ContextHierarchy
are mixed on single class
This commit provides first-class support for Bean Overrides (@MockitoBean, @MockitoSpyBean, @TestBean, etc.) with @ContextHierarchy. Specifically, bean overrides can now specify which ApplicationContext they target within the context hierarchy by configuring the `contextName` attribute in the annotation. The `contextName` must match a corresponding `name` configured via @ContextConfiguration. For example, the following test class configures the name of the second hierarchy level to be "child" and simultaneously specifies that the ExampleService should be wrapped in a Mockito spy in the context named "child". Consequently, Spring will only attempt to create the spy in the "child" context and will not attempt to create the spy in the parent context. @ExtendWith(SpringExtension.class) @ContextHierarchy({ @ContextConfiguration(classes = Config1.class), @ContextConfiguration(classes = Config2.class, name = "child") }) class MockitoSpyBeanContextHierarchyTests { @MockitoSpyBean(contextName = "child") ExampleService service; // ... } See spring-projectsgh-33293 See spring-projectsgh-34597
This commit provides first-class support for Bean Overrides (@MockitoBean, @MockitoSpyBean, @TestBean, etc.) with @ContextHierarchy. Specifically, bean overrides can now specify which ApplicationContext they target within the context hierarchy by configuring the `contextName` attribute in the annotation. The `contextName` must match a corresponding `name` configured via @ContextConfiguration. For example, the following test class configures the name of the second hierarchy level to be "child" and simultaneously specifies that the ExampleService should be wrapped in a Mockito spy in the context named "child". Consequently, Spring will only attempt to create the spy in the "child" context and will not attempt to create the spy in the parent context. @ExtendWith(SpringExtension.class) @ContextHierarchy({ @ContextConfiguration(classes = Config1.class), @ContextConfiguration(classes = Config2.class, name = "child") }) class MockitoSpyBeanContextHierarchyTests { @MockitoSpyBean(contextName = "child") ExampleService service; // ... } See spring-projectsgh-33293 See spring-projectsgh-34597 Signed-off-by: Sam Brannen <[email protected]>
This commit provides first-class support for Bean Overrides (@MockitoBean, @MockitoSpyBean, @TestBean, etc.) with @ContextHierarchy. Specifically, bean overrides can now specify which ApplicationContext they target within the context hierarchy by configuring the `contextName` attribute in the annotation. The `contextName` must match a corresponding `name` configured via @ContextConfiguration. For example, the following test class configures the name of the second hierarchy level to be "child" and simultaneously specifies that the ExampleService should be wrapped in a Mockito spy in the context named "child". Consequently, Spring will only attempt to create the spy in the "child" context and will not attempt to create the spy in the parent context. @ExtendWith(SpringExtension.class) @ContextHierarchy({ @ContextConfiguration(classes = Config1.class), @ContextConfiguration(classes = Config2.class, name = "child") }) class MockitoSpyBeanContextHierarchyTests { @MockitoSpyBean(contextName = "child") ExampleService service; // ... } See spring-projectsgh-33293 See spring-projectsgh-34597 See spring-projectsgh-34726 Signed-off-by: Sam Brannen <[email protected]>
This commit provides first-class support for Bean Overrides (@MockitoBean, @MockitoSpyBean, @TestBean, etc.) with @ContextHierarchy. Specifically, bean overrides can now specify which ApplicationContext they target within the context hierarchy by configuring the `contextName` attribute in the annotation. The `contextName` must match a corresponding `name` configured via @ContextConfiguration. For example, the following test class configures the name of the second hierarchy level to be "child" and simultaneously specifies that the ExampleService should be wrapped in a Mockito spy in the context named "child". Consequently, Spring will only attempt to create the spy in the "child" context and will not attempt to create the spy in the parent context. @ExtendWith(SpringExtension.class) @ContextHierarchy({ @ContextConfiguration(classes = Config1.class), @ContextConfiguration(classes = Config2.class, name = "child") }) class MockitoSpyBeanContextHierarchyTests { @MockitoSpyBean(contextName = "child") ExampleService service; // ... } See spring-projectsgh-33293 See spring-projectsgh-34597 See spring-projectsgh-34726 Signed-off-by: Sam Brannen <[email protected]>
This commit provides first-class support for Bean Overrides (@MockitoBean, @MockitoSpyBean, @TestBean, etc.) with @ContextHierarchy. Specifically, bean overrides can now specify which ApplicationContext they target within the context hierarchy by configuring the `contextName` attribute in the annotation. The `contextName` must match a corresponding `name` configured via @ContextConfiguration. For example, the following test class configures the name of the second hierarchy level to be "child" and simultaneously specifies that the ExampleService should be wrapped in a Mockito spy in the context named "child". Consequently, Spring will only attempt to create the spy in the "child" context and will not attempt to create the spy in the parent context. @ExtendWith(SpringExtension.class) @ContextHierarchy({ @ContextConfiguration(classes = Config1.class), @ContextConfiguration(classes = Config2.class, name = "child") }) class MockitoSpyBeanContextHierarchyTests { @MockitoSpyBean(contextName = "child") ExampleService service; // ... } See spring-projectsgh-33293 See spring-projectsgh-34597 See spring-projectsgh-34726 Signed-off-by: Sam Brannen <[email protected]>
@wilkinsona, I wanted to take a look at the prototype you created for Spring Boot, but that branch unfortunately no longer exists. In any case, I have prototyped a solution for Spring Framework's bean override support, and I would appreciate it if you could take a look at #34723 and let me know your thoughts on the proposal. Thanks in advance! |
I have put considerable thought into this and created #34723 to address this and similar issues. So, feel free to subscribe to that PR for further updates. In light of that, I am closing this issue as: |
This commit provides first-class support for Bean Overrides (@MockitoBean, @MockitoSpyBean, @TestBean, etc.) with @ContextHierarchy. Specifically, bean overrides can now specify which ApplicationContext they target within the context hierarchy by configuring the `contextName` attribute in the annotation. The `contextName` must match a corresponding `name` configured via @ContextConfiguration. For example, the following test class configures the name of the second hierarchy level to be "child" and simultaneously specifies that the ExampleService should be wrapped in a Mockito spy in the context named "child". Consequently, Spring will only attempt to create the spy in the "child" context and will not attempt to create the spy in the parent context. @ExtendWith(SpringExtension.class) @ContextHierarchy({ @ContextConfiguration(classes = Config1.class), @ContextConfiguration(classes = Config2.class, name = "child") }) class MockitoSpyBeanContextHierarchyTests { @MockitoSpyBean(contextName = "child") ExampleService service; // ... } See spring-projectsgh-33293 See spring-projectsgh-34597 See spring-projectsgh-34726 Signed-off-by: Sam Brannen <[email protected]>
Hi,
attached is a testcase showing a testcontest that is wrongly reloaded when using
@MockBean
in conjunction with using@ContextHierarchy
in single class mode.The testcase is in the attached zip, in the "notworking" folder. the "working" folder is present only for comparison to illustrate the bug.
run $
mvn package
at the root to run the testcases.mockbean-hiearchy-singleclass-bug.zip
the testcase shows that a context that should be cached is instead reloaded when multiple classes using the same context as the parent of a contexthiearchy, and using mockbean on a bean in a child context (so the parent should still be reusable):
DOESN'T WORK
The bug does not happen when using @ContextHiearchy with subclassing:
WORKS
This prevents reliable context caching, useful for example when expensive beans (like test databases and databases connections) are created only once in a parent context for every test classes.
The text was updated successfully, but these errors were encountered: