You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Convert TestContext to interface and introduce default implementation [SPR-7692] #12348
Apparently, when using Mockito, the primary (if not the only) thing needed for mocks to work is making the public methods non-final. I had no chance to verify that, but probably the constructor could remain hidden (not sure what this is for anyway).
If making methods non-final is indeed the only thing required -- then, fortunately, this is a really simple change.
workaround
In a comment to #10102 I wrote a reflection based workaround for the inability to mock the TestContext. Lengthy and obscure, of course.
Since the Spring TestContext Framework was introduced in Spring
Framework 2.5, the TestContext class has always been a public class
with package private constructors. The visibility of TestContext's
constructor and methods was intentionally limited in order to hide the
implementation details of the context cache, etc. However, this fact
has made it difficult (if not impossible) to unit test custom
TestExecutionListener implementations.
This commit addresses this issue by converting TestContext into a
public interface with a package private DefaultTestContext
implementation. This enables unit testing of any components that depend
on a TestContext (e.g., TestExecutionListeners) while at the same time
preserving the encapsulation of the inner workings of the TestContext
implementation with regard to context loading and caching.
The fact that TestExecutionListeners are not testable is a paintful limitation of Spring 3.x. Would it be possible to backport this issue to 3.2.5?
If the change happens to be too big to be backported, what about just removing final keyword from methods in TestContext class? That would not harm isolation, since TestContext would retain a package-private ctor, and thus would still be under your full control.
Activity
spring-projects-issues commentedon Oct 16, 2013
Piotr Findeisen commented
I second the request.
Apparently, when using Mockito, the primary (if not the only) thing needed for mocks to work is making the public methods non-final. I had no chance to verify that, but probably the constructor could remain hidden (not sure what this is for anyway).
If making methods non-final is indeed the only thing required -- then, fortunately, this is a really simple change.
workaround
In a comment to #10102 I wrote a reflection based workaround for the inability to mock the
TestContext. Lengthy and obscure, of course.spring-projects-issues commentedon Oct 26, 2013
Sam Brannen commented
This has been addressed as described in the comments for GitHub commit 88fe2e9b00.
spring-projects-issues commentedon Oct 27, 2013
Piotr Findeisen commented
The fact that TestExecutionListeners are not testable is a paintful limitation of Spring 3.x. Would it be possible to backport this issue to 3.2.5?
If the change happens to be too big to be backported, what about just removing
finalkeyword from methods inTestContextclass? That would not harm isolation, sinceTestContextwould retain a package-private ctor, and thus would still be under your full control.spring-projects-issues commentedon Feb 22, 2014
Sam Brannen commented
Hi Piotr,
I forgot to mention this earlier, but I did in fact implement the limited "non-final" change in Spring Framework 3.2.6.
Please refer to the comments for GitHub commit b20b741 for details:
Regards,
Sam
spring-projects-issues commentedon Mar 4, 2014
Piotr Findeisen commented
Hi Sam,
works like a charm -- I'm using Spring 3.2.8 now.
Thanks!
spring-projects-issues commentedon Mar 4, 2014
Sam Brannen commented
Piotr, thanks for the feedback, and of course... I'm glad it's working nicely for you! :)