Skip to content

Runtime resolver for @ActiveProfiles #240

Closed

Description

@michail-nikolaev

Hello.
I my current project I often want to resolve test active profiles at the runtime.
For example: I want to put package-level annotation to the use same profiles for bulk of tests without manual annotating them (it is not possible annotate base class because tricky inheritance in test classes).
Currently it is looks impossible to do such trick correctly using any documented way (test execution listeners or inheritance from SpringJUnit4ClassRunner) without marking every test as @DirtyContext.

So, I wonder is it possible to extend @ActiveProfiles annotation (or add a new one) to do something like this:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ActiveProfiles(resolver = SomeActiveProfileResolver.class)
    class SomeClass() {}

    class SomeActiveProfileResolver implement ActiveProfileResolver {
           @Override
           public String[] resolveProfiles(Object testInstance) {
                  // some logic
           }
    }

    interface ActiveProfileResolver {
        String[] resolveProfiles(Object testInstance);
    }

I will happy to contribute implementation if you agree with idea.

Thanks a lot.

Activity

  1. sbrannen commented on Feb 27, 2013

    @sbrannen
    Member

    If you need to programmatically configure active profiles for integration tests, the best approach is to create a custom ApplicationContextInitializer and declare that via @ContextConfiguration(..., initializers = MyCustomContextInitializer.class).

    I will therefore close this issue.

    Regards,

    Sam

  2. michail-nikolaev commented on Feb 27, 2013

    @michail-nikolaev
    Author

    Thanks for your advice.

    But as I know usage ApplicationContextInitializer will lead to disabling caching of Spring context (which is implemented in TestContextManager). So it will make testing too slow.

    Regards,
    Michail.

  3. sbrannen commented on Feb 27, 2013

    @sbrannen
    Member
  4. michail-nikolaev commented on Feb 27, 2013

    @michail-nikolaev
    Author

    You're right, but only the concrete implementation type of ApplicationContextInitializer is used for caching.

    Consequently, a given concrete ApplicationContextInitializer implementation is semantically not able to first initialize a context using profiles "A, B" and then to later initialize a subsequent context using only profile "A", since the first context will have been cached based on the concrete initializer type.

    It is therefore impossible to programmatically select profiles depending on information retrieved from the test class instance (e.g., based on the parent package).

    I think an ActiveProfileResolver could be very useful in some cases, but you are the judge here. :)

    Thanks for you help.

  5. sbrannen commented on Feb 27, 2013

    @sbrannen
    Member

    I hope you don't mind that I edited your last comment for greater clarity. If I misinterpreted your original intent, please let me know.

  6. sbrannen commented on Feb 27, 2013

    @sbrannen
    Member

    Note: if an ActiveProfileResolver is not required to perform the exact same resolution every time it is invoked, that would mean that the profile resolution algorithm is nondeterministic. Consequently, a context loaded using an ActiveProfileResolver would not be able to be stored in the context cache, thereby requiring that each such test class be annotated with @DirtiesContext.

    So, how do you envision that an ActiveProfileResolver would be used for determining the context cache key (i.e., in MergedContextConfiguration)?

    Or are you suggesting that any context loaded using an ActiveProfileResolver simply not be cached at all?

  7. sbrannen commented on Feb 27, 2013

    @sbrannen
    Member

    Reopening for further discussion...

  8. michail-nikolaev commented on Feb 27, 2013

    @michail-nikolaev
    Author

    First thanks a lot for attention and for editing my terrible english.

    The main idea does not include ActiveProfileResolver in MergedContextConfiguration hashcode but to include ActiveProfileResolver.resolve() invocation result.

    Let change ActiveProfileResolver interface to this (resolve using class rather than instance - generally it is almost same, because instance will be without @injects anyway):

    interface ActiveProfileResolver {
        String[] resolve(Class<?> testClass);
    }
    

    In such case to achive correct context caching and dynamic profiles we just need in ContextLoaderUtils.resolveActiveProfiles check for @ActiveProfiles.resolver and call its String[] resolve(...) method (instead of getting profiles from @ActiveProfiles.profiles). So, same String[] resolve(...) results will lead to same MergedContextConfiguration instances (in case of other fields equality) and positive cache matches in TestContext.getApplicationContext.

  9. sbrannen commented on Feb 27, 2013

    @sbrannen
    Member

    OK. That makes a lot more sense.

    Thanks for the clarification!

    Could you please create a JIRA issue entitled "Introduce ActiveProfilesResolver in the TestContext framework" and assign it to the "TEST" component?

    Thanks in advance,

    Sam

    p.s. I'm now closing this issue since it will be tracked in the JIRA issue you create.

  10. michail-nikolaev commented on Feb 27, 2013

    @michail-nikolaev
    Author
  11. added a commit that references this issue on Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions