Skip to content
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

SpringApplicationContextLoader ignores Application class [SPR-11628] #16251

Closed
spring-projects-issues opened this issue Mar 30, 2014 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: test Issues in the test module status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Mar 30, 2014

Abhijit Sarkar opened SPR-11628 and commented

The SpringApplicationContextLoader assumes that the application is either using 100% XML or 100% Java config. This is because @ContextConfiguration allows either a list of classes or locations/value, not both. If any is specified, SpringApplicationContextLoader ignores the Application class that creates and starts the SpringApplication.

Trying to make Boot work with a 100% Groovy/no-XML pet project, I ran across the above issue. My Application class has @EnableAutoConfiguration and @ComponentScan annotations on it, the former required by Boot to set up a Web server. The later I had to keep because of #16250. On the other hand, if I omitted the locations/value on @ContextConfiguration, dependencies weren't set up (duh!).

I give the code below along with a patch that I locally made to SpringApplicationContextLoader.

MovieDatabaseRESTClientIntegrationTest.groovy

RunWith(SpringJUnit4ClassRunner)
@ContextConfiguration(value = ['classpath:client-config.groovy', 'classpath:integ-test-config.groovy'],
        loader = PatchedSpringApplicationContextLoader)
@SpringApplicationConfiguration(classes = MovieDatabaseApplication)
@WebAppConfiguration
@IntegrationTest
class MovieDatabaseRESTClientIntegrationTest {

MovieDatabaseApplication.groovy

@EnableAutoConfiguration
@ComponentScan
class MovieDatabaseApplication {

SpringApplicationContextLoader.java fix

private Set<Object> getSources(MergedContextConfiguration mergedConfig) {
        Set<Object> sources = new LinkedHashSet<Object>();
        sources.addAll(Arrays.asList(mergedConfig.getClasses()));
        sources.addAll(Arrays.asList(mergedConfig.getLocations()));

        /* The Spring application class may have annotations on it too. If such a class is declared on the test class,
        * add it as a source too. */
        SpringApplicationConfiguration springAppConfig = AnnotationUtils.findAnnotation(mergedConfig.getTestClass(),
                SpringApplicationConfiguration.class);

        if (springAppConfig != null) {
            sources.addAll(Arrays.asList(springAppConfig.classes()));
        }

        if (sources.isEmpty()) {
            throw new IllegalStateException(
                    "No configuration classes or locations found in @SpringApplicationConfiguration. "
                            + "For default configuration detection to work you need Spring 4.0.3 or better (found "
                            + SpringVersion.getVersion() + ").");
        }
        return sources;
    }

Affects: 4.0.3

Reference URL: http://forum.spring.io/forum/spring-projects/boot/746963-springapplicationcontextloader-ignores-application-class

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 30, 2014

Sam Brannen commented

I'm resolving this issue as Won't Fix, since the problem described is related solely to Spring Boot, not to the core Spring Framework or Spring TestContext Framework.

Please open an issue on GitHub for Spring Boot.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 30, 2014

Abhijit Sarkar commented

Done. Also submitted a pull request.
spring-boot-issue-603

@spring-projects-issues spring-projects-issues added type: bug A general bug status: declined A suggestion or change that we don't feel we should currently apply in: test Issues in the test module in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: test Issues in the test module status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants