-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Open
Labels
in: testIssues in the test moduleIssues in the test moduletype: enhancementA general enhancementA general enhancement
Milestone
Description
Rob Winch opened SPR-12031 and commented
Status Quo
The Spring TestContext Framework (TCF) makes it very easy to run tests that share the same configuration within a test class hierarchy or even across a test suite. However, it does not support annotating a method with @ContextConfiguration
.
Proposals
- It would be nice if the TCF would allow
@ContextConfiguration
on a test method. This would allow for easily testing various permutations of configuration. - Support throw-away contexts (i.e., contexts that are loaded for a single test method but not stored in the context cache), perhaps via a separate issue that serves as a building block for this issue.
Issue Links:
- Support Verifying Errors with @ContextConfiguration at method level [SPR-15034] #19600
- Support @ActiveProfiles at method level [SPR-9338] #13977
- Support @TestPropertySource at method level [SPR-14378] #18951
- Support 'throw-away' context for a test class [SPR-13722] #18295
- SPR-12031 Support @ContextConfiguration at method level #1255
Referenced from: commits spring-attic/spring-framework-issues@73db0e2
7 votes, 14 watchers
daniel-shuy and JanecekPetrjbp198669
Metadata
Metadata
Assignees
Labels
in: testIssues in the test moduleIssues in the test moduletype: enhancementA general enhancementA general enhancement
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
spring-projects-issues commentedon Jul 25, 2014
Sam Brannen commented
Dave Syer made a similar suggestion in the comments for #10284.
Since JIRA's permalink functionality seems to be broken, I am including Dave's comments here:
spring-projects-issues commentedon Jul 25, 2014
Sam Brannen commented
Hi guys,
A few brainstorming questions...
TestExecutionListeners
, etc.@ContextHierarchy
)?@Configuration
class be detected for an empty method-level declaration of@ContextConfiguration
?Cheers,
Sam
spring-projects-issues commentedon Jun 30, 2015
Thomas Darimont commented
Continuing the discussions from #12387:
@1
:I'd just let them override bean definitions... - w.r.t.
TestExecutionListeners
:What would be some use cases where users wanted to customize TELs (indirectly via config classes) on test method level?
@2
:How about using the method level java config as an overlay / decorator for the top of the context hierarchy stack? This would enable you to replace / override beans but would still behave in an expected way.
Perhaps we need another annotation here than
@ContextConfiguration
if we wouldn't want to allow all that CC allows on method level...@3
. Should method-level contexts be cached at all?I think caching them is possible but could lead to subtible bugs or at least unexpected behaviour. I think it would be okay to not cache those in the first
iteration.
@4
.Perhaps based on some convention?
Should lookup something like config
Test_with_awkward_but_descriptive_nameConfig{
} or aSomeTests_test_with_awkward_but_descriptive_name.xml
That the latter could be quite handy IMHO - the former as well if you could specifiy a the test class to derive in a sane way ;-) - perhaps via an annotation based qualifier - which would also be more robust against refactorings than a test method name based convention.
spring-projects-issues commentedon Jun 30, 2015
Sam Brannen commented
Hi Thomas,
Thanks for the valuable feedback!
I won't have any time to work on this issue for quite some time (perhaps months since it's too late for 4.2), but that doesn't mean it is forgotten. After all, it's still in my Waiting for Triage list. ;)
Regards,
Sam
spring-projects-issues commentedon Jun 30, 2015
Sam Brannen commented
In the interim, if you are interested in seeing something like this implemented in
spring-test
, feel free to vote for this issue.spring-projects-issues commentedon Aug 19, 2015
Rob Winch commented
Sam Brannen I would really love to be able to clean up up my tests. Any chance we could see this feature soon?
If not, I don't suppose you have a good idea of how to work around this limitation? I'm fine if the workaround does not support caching.
spring-projects-issues commentedon Aug 19, 2015
Sam Brannen commented
Hi Rob Winch,
I don't foresee this getting implemented in the very near future, perhaps in Spring Framework 4.3 at the earliest since it requires quite a lot of changes to the internals of the TCF (to do it right and make it a first-class feature).
In the interim, however, it should be possible with custom
TestContext
andTestContextBootstrapper
implementations.Warning: I have not tried this, but I believe the following should work...
DefaultTestContext
.MergedContextConfiguration
in its own instance field sincemergedContextConfiguration
is aprivate
field inDefaultTestContext
.MergedContextConfiguration
for the current test method and set theparent
of that MCC to the MCC passed into your constructor. The parent part is optional, depending on your needs of course.getApplicationContext()
andmarkApplicationContextDirty()
methods, essentially by copying the existing code from the corresponding overridden methods but using your custom method-level MCC instead of the standard class-level MCC.AbstractTestContextBootstrapper
(or preferably a subclass ofDefaultTestContextBootstrapper
orWebTestContextBootstrapper
) which overrides thebuildTestContext()
method and returns an instance of the customTestContext
you created in step # 1 above.TestContextBootstrapper
(e.g., via@BootstrapWith
).I suppose the biggest obstacle is that
@ContextConfiguration
is currently declared with@Target(ElementType.TYPE)
, meaning that you cannot declare it on a test method. As a consequence, to build yourMergedContextConfiguration
in step # 1 you will have to find another way to declare and obtain the necessary metadata (e.g., via a custom method-level annotation that mimics@ContextConfiguration
, providing likely a subset of its features).Does that make sense?
Is that enough for you to go on?
If you make any progress based on these tips, I'd be delighted to take a look at it. ;)
Cheers,
Sam
spring-projects-issues commentedon Aug 19, 2015
Rob Winch commented
Sam Brannen Thanks for the detailed writeup! This does make sense. I will take a look into implementing this soon.
spring-projects-issues commentedon Aug 19, 2015
Sam Brannen commented
You're welcome Rob Winch, and... may the source be with you! ;)
spring-projects-issues commentedon Sep 28, 2015
Rob Winch commented
Thanks to your tips I was able to get a good start on this! I have a few questions though.
REINJECT_DEPENDENCIES_ATTRIBUTE
attribute.spring-projects-issues commentedon Oct 9, 2015
Sam Brannen commented
Hi Rob Winch,
Glad to hear you're making progress! :)
Thanks to your tips I was able to get a good start on this! I have a few questions though.
No, not currently.
prepareTestInstance()
always injects dependencies.Nothing other than what you already hinted at:
REINJECT_DEPENDENCIES_ATTRIBUTE
.That's how both
DirtiesContextTestExecutionListener
andDirtiesContextBeforeModesTestExecutionListener
instruct theDependencyInjectionTestExecutionListener
to perform DI before test methods. This feature was added for TestNG, since TestNG does not reinstantiate the test class between methods (in contrast to JUnit).So one option would be to implement a custom
TestExecutionListener
that sets theREINJECT_DEPENDENCIES_ATTRIBUTE
totrue
in itsbeforeTestMethod()
method. Then order that TEL just before the DITEL.Make sense?
spring-projects-issues commentedon Oct 9, 2015
Rob Winch commented
Sam Brannen Once again thank you for your response.
A follow up question that was in the javadoc, but not listed in the comments. Is there an easy way to easily include all default TestExecutionListeners, but replace DependencyInjectionTestExecutionListener with my custom one?
Thanks again!
spring-projects-issues commentedon Oct 9, 2015
Sam Brannen commented
No, unfortunately not.
I guess you could say that's a missing feature.
With Spring Security (which I'm pretty sure you're familiar with ;) ), there's an option for replacing a filter in the filter chain; however, the TestContext framework only provides the ability to insert an additional
TestExecutionListener
... at least via first-class mechanisms.There is, however, a hackish way to achieve this goal. I described it somewhere in an issue for Spring Boot (in case you want to search for it). In summary: you could override
AbstractTestContextBootstrapper.getDefaultTestExecutionListenerClasses()
, delegate tosuper.getDefaultTestExecutionListenerClasses()
, and then manually filter the default listeners to your heart's content.- Sam
spring-projects-issues commentedon Oct 16, 2015
Dave Syer commented
Here's some code that works for me: spring-attic/spring-framework-issues#110. It would be great to see support for this pattern in spring-test.
spring-projects-issues commentedon Oct 17, 2015
Sam Brannen commented
Thanks for sharing your working example, Dave!
Since both you and Rob have made progress in this area, I'll go ahead and move this issue to the 4.3 backlog for more immediate consideration.
15 remaining items
spring-projects-issues commentedon Dec 17, 2016
Sam Brannen commented
Rob Winch,
Could you please expound on that? And yes... in a separate issue.
Thanks
spring-projects-issues commentedon Dec 19, 2016
Rob Winch commented
Thanks Sam Brannen I provided SPR-15034 to track verifying configuration errors.
@DynamicPropertySource
on non-static methods #24825