Skip to content

Support declarative PropertySource annotations in the TestContext framework [SPR-11377] #16004

Closed

Description

@spring-projects-issues

Shevek opened SPR-11377 and commented

I wrote this ages ago, but:

There should be an equivalent of @ActiveProfiles for PropertySources. I wrote this code, and hope it helps.

public class CustomTestContextLoader extends DelegatingSmartContextLoader {

    private final SmartContextLoader annotationConfigLoader = new AnnotationConfigContextLoader() {
        @Override
        protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
            super.prepareContext(context, mergedConfig);
            Class<?> testClass = mergedConfig.getTestClass();

            Map<String, Object> properties = new HashMap<String, Object>();

            TestPropertyValues values = AnnotationUtils.getAnnotation(testClass, TestPropertyValues.class);
            if (values != null)
                for (TestPropertyValue value : values.value())
                    properties.put(value.name(), value.value());

            TestPropertyValue value = AnnotationUtils.getAnnotation(testClass, TestPropertyValue.class);
            if (value != null)
                properties.put(value.name(), value.value());

            if (!properties.isEmpty()) {
                MapPropertySource source = new MapPropertySource(TestPropertyValues.class.getSimpleName(), properties);
                context.getEnvironment().getPropertySources().addFirst(source);
            }
        }
    };

    @Override
    protected SmartContextLoader getAnnotationConfigLoader() {
        return annotationConfigLoader;
    }
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TestPropertyValues {

    TestPropertyValue[] value();
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface TestPropertyValue {

    String name();

    String value();
}

Then it's used like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
        loader = CustomTestContextLoader.class,
        classes = {...})
@TestPropertyValues({
    @TestPropertyValue(name = "key", value = "value"),
    @TestPropertyValue(name = "foo", value = "bar")
})
public class MyTest { ... }

Can we have a better integrated equivalent of this in the default DelegatingSmartContextLoader, or wherever you feel appropriate, please? It would help us IMMENSELY.

Thank you.


Affects: 3.2.7

Issue Links:

Activity

  1. spring-projects-issues commented on Jan 31, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Shevek commented

    Wow, Jira mangled that code somewhat. But it's all there. :-)

  2. spring-projects-issues commented on Jan 31, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Please note that this is strongly related to #14865.

  3. spring-projects-issues commented on Jan 31, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Shevek commented

    Strongly related, but very much different, as this keeps everything in one file, and #14865 seems to require two files minimum which must be kept in sync.

    This particular fragment of code saves us a LOT of time and energy.

  4. spring-projects-issues commented on Jul 31, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Please note that this issue has been superseded by #16667.

  5. spring-projects-issues commented on Aug 13, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    If you are following this issue, you might be interested in knowing that #16667 has been resolved.

  6. added
    in: test
    type: enhancement
    status: duplicate
    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: teststatus: duplicatetype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions