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
This commit is contained in:
Andy Wilkinson 2015-05-11 15:43:44 +01:00
parent 2053f4b2bf
commit 4977e48ec5
1 changed files with 2 additions and 0 deletions

View File

@ -68,6 +68,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
*
* @author Dave Syer
* @author Phillip Webb
* @author Andy Wilkinson
* @see IntegrationTest
* @see WebIntegrationTest
* @see TestRestTemplate
@ -81,6 +82,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
throws Exception {
assertValidAnnotations(config.getTestClass());
SpringApplication application = getSpringApplication();
application.setMainApplicationClass(config.getTestClass());
application.setSources(getSources(config));
ConfigurableEnvironment environment = new StandardEnvironment();
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {