Skip to content

Convert TestContext to interface and introduce default implementation [SPR-7692] #12348

Closed

Description

@spring-projects-issues

Jaro Kuruc opened SPR-7692 and commented

I am trying to unit test a custom TestExecutionListener, but it's impossible to do it because:

  1. TestContext is a class and not interface, so can't be mocked without using ClassImposterizer
  2. TestContext constructors require ContextCache, which is package private so can't be instantiated/mocked in a unit test.

Please make this more flexible.


Affects: 3.0.5

Issue Links:

Referenced from: commits 88fe2e9

Activity

  1. spring-projects-issues commented on Oct 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    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.

  2. spring-projects-issues commented on Oct 26, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    This has been addressed as described in the comments for GitHub commit 88fe2e9b00.

    Convert TestContext to interface & default impl

    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.

  3. spring-projects-issues commented on Oct 27, 2013

    @spring-projects-issues
    CollaboratorAuthor

    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 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.

  4. spring-projects-issues commented on Feb 22, 2014

    @spring-projects-issues
    CollaboratorAuthor

    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:

    Make methods of TestContext non-final

    This commit makes several methods of TestContext non-final in order to
    allow TestContext to be mocked — for example with Mockito.

    Note, however, that TestContext has been converted to an interface in
    Spring Framework 4.0.

    Regards,

    Sam

  5. spring-projects-issues commented on Mar 4, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Piotr Findeisen commented

    Hi Sam,

    works like a charm -- I'm using Spring 3.2.8 now.
    Thanks!

  6. spring-projects-issues commented on Mar 4, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Piotr, thanks for the feedback, and of course... I'm glad it's working nicely for you! :)

  7. added
    in: test
    type: enhancement
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: testtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions