-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Provide support for context hierarchies in the TestContext Framework [SPR-5613] #10284
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
Comments
Dave Syer commented I have a suggestion: what about extending This would also be a great feature for integration testing where you want to override a handful of beans just for the test to provide a stubbed or alternative environment, or where you want to test multiple profiles in one go, without having to reset system properties. |
Tadaya Tsuyukubo commented different implementation of the comment above, but I wrote a code to support parent app context for spring test framework. pull request here: |
Neale Upstone commented Looking forward to seeing #9309 resolved by this, with the test context always as a child of the context specified in |
Sam Brannen commented If you are watching this issue, please feel free to participate in the discussion regarding the proposed Deliverables and the corresponding Pseudocode Examples listed in this issue's Description. Thanks! Sam |
Tadaya Tsuyukubo commented Hi Sam, I just want to poke around corner-case behaviors:
In ExtendedTests, I didn't specify the name on the In this case, three contexts with this hierarchy to be expected? Another case:
Does this merge the configuration(parent.xml and child.xml)? |
Sam Brannen commented Hi Tadaya, Thanks for the feedback! The answers to your questions are "yes" and "no", respectively. Based on the current design, merging and overriding is only possible if the contexts in question are explicitly named; otherwise, unnamed contexts (or contexts with a different name) are considered to define separate levels in the hierarchy, following a top-down approach based on the location of the So, in your examples you would get:
Note that the Regards, Sam |
Sam Brannen commented Please note that I've added Resulting Hierarchies ASCII art to help everyone visualize the semantics of the proposal. - Sam |
Tadaya Tsuyukubo commented Thank you, Sam. Another question: sample:
is considered as this?
Based on the answer above, I think result of the following would be different:
Regards, |
Sam Brannen commented Tadaya, Thanks again for your feedback. You are raising some very good questions! ;)
In general, no, For backwards compatibility, the semantics for a stand-alone However,
Yes, within a class hierarchy that declares context hierarchies, a bare For example, if I'll add some more examples in the Pseudocode Examples section to make this clearer.
The above example has been a valid configuration since Spring 2.5. Thus the semantics for this kind of setup will remain unchanged. Specifically, no context hierarchy will be created. Instead, two contexts will be loaded, one for Regards, Sam |
Sam Brannen commented Hi everybody, If you're actively following this issue, you may be interested in a sneak peak at the current code in my GitHub repository: https://github.com/sbrannen/spring-framework/tree/SPR-5613 The only part that is currently missing is support for dirtying a context (with So feel free to take it for a spin... Feedback is welcome! Sam |
Tadaya Tsuyukubo commented Hi Sam, I was looking your sneakpeak code and implementing the I was thinking the usage to be like this:
The above example would mark "bar" and it's child contexts such as "baz" as dirty. If this is the expected behavior, I'll finish up the implementation and send a pull request. Thanks, |
Tadaya Tsuyukubo commented Hi Sam, pull request is here: Thanks, |
Sam Brannen commented Tadaya Tsuyukubo, thanks for submitting the pull request! Unfortunately, using the names of context hierarchy levels to clear the context cache is fundamentally flawed. Please see my comments in your pull request for details. Regards, Sam |
Sam Brannen commented Submitted pull request: #247 |
Sam Brannen commented Completed as described in the comments for GitHub commit 4c5d771764:
|
Tomasz Wysocki opened SPR-5613 and commented
Status Quo
Currently the TestContext framework supports creating only flat, non-hierarchical contexts. There is no easy way to create contexts with parent-child relationships.
Goals
Example Hierarchies
WebApplicationContext
<-- DispatcherWebApplicationContext
ApplicationContext
<-- RootWebApplicationContext
<-- DispatcherWebApplicationContext
Deliverables
@ContextHierarchy
annotation that can contain nested@ContextConfiguration
declarationsname
attribute in@ContextConfiguration
that can be used for merging or overriding named@ContextConfiguration
elements in the context hierarchy@ContextHierarchy
in a single test class@ContextHierarchy
and@ContextConfiguration
within the test class hierarchyname
attribute of@ContextConfiguration
name
andinheritLocations
attributes of@ContextConfiguration
Implementation Considerations
MergedContextConfiguration
should have a reference to the parentMergedContextConfiguration
(i.e., aprivate final
field)MergedContextConfiguration
should provide apublic ApplicationContext getParentApplicationContext()
method thatSmartContextLoader
instances can use to retrieve and set the parentApplicationContext
MergedContextConfiguration
will need an internal reference to the context cache (or at least a way to retrieve the parent context from the cache)Pseudocode Examples
Single Test Class with Context Hierarchy
Resulting Hierarchies
Class Hierarchy with Implicit Parent Context
Resulting Hierarchies
Class Hierarchy with Bare
@ContextConfiguration
in a SuperclassIn this scenario it is assumed that
AbstractTests
existed prior to the introduction of@ContextHierarchy
support and that the author does not wish to (or cannot) modifyAbstractTests
to declare a@ContextHierarchy
.Resulting Hierarchies
Class Hierarchy with Bare
@ContextConfiguration
in a SubclassThis scenario is not necessarily recommended, but it should still be supported for the sake of consistency.
Ideally, if the author of a test class knows that the context for the test class should take part in a context hierarchy, then the author should explicitly declare
@ContextConfiguration
within@ContextHierarchy
.Resulting Hierarchies
Class Hierarchy with Merged Configuration
Resulting Hierarchies
Class Hierarchy with Overridden Configuration
Resulting Hierarchies
Affects: 2.5.6
Issue Links:
Referenced from: commits eefd1c4, 4c5d771, 98074e7
10 votes, 11 watchers
The text was updated successfully, but these errors were encountered: