Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Jan 31, 2014 · 5 comments
Assignees
Labels
in: test Issues in the test module status: duplicate A duplicate of another issue type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jan 31, 2014

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:

@spring-projects-issues
Copy link
Collaborator Author

Shevek commented

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

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 31, 2014

Sam Brannen commented

Please note that this is strongly related to #14865.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 31, 2014

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.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jul 31, 2014

Sam Brannen commented

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

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Aug 13, 2014

Sam Brannen commented

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants