Commit 4977e48e authored by Andy Wilkinson's avatar Andy Wilkinson

Don’t rely on deduction of main application class when running tests

Previously, when SpringApplicationContextLoader created a
SpringApplication and used it to load the test’s application context,
it relied upon SpringApplication correctly deducing the application’s
main class. This would result in the wrong class being deduced as the
application’s main method is not called so the test runner’s main method
would be found instead.

This commit updates SpringApplicationContextLoader to explicitly set
SpringApplication’s main class to be the current test’s class. While
not strictly the application’s main class, it is the next best thing
available in this situation and prevents misleading log messages and
application versions from being logged.

Fixes gh-2930
parent 2053f4b2
...@@ -68,6 +68,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext; ...@@ -68,6 +68,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
* *
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson
* @see IntegrationTest * @see IntegrationTest
* @see WebIntegrationTest * @see WebIntegrationTest
* @see TestRestTemplate * @see TestRestTemplate
...@@ -81,6 +82,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader { ...@@ -81,6 +82,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
throws Exception { throws Exception {
assertValidAnnotations(config.getTestClass()); assertValidAnnotations(config.getTestClass());
SpringApplication application = getSpringApplication(); SpringApplication application = getSpringApplication();
application.setMainApplicationClass(config.getTestClass());
application.setSources(getSources(config)); application.setSources(getSources(config));
ConfigurableEnvironment environment = new StandardEnvironment(); ConfigurableEnvironment environment = new StandardEnvironment();
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) { if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment