Merge pull request #33371 from lishangbu

* 33371:
  Avoid NPE when @SpringBootConfiguration not found in test

Closes gh-33371
This commit is contained in:
Moritz Halbritter 2022-12-01 11:23:06 +01:00
commit c8d2200fc8
1 changed files with 2 additions and 2 deletions

View File

@ -244,8 +244,6 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
return classes; return classes;
} }
Class<?> found = findConfigurationClass(mergedConfig.getTestClass()); Class<?> found = findConfigurationClass(mergedConfig.getTestClass());
Assert.state(found != null, "Unable to find a @SpringBootConfiguration, you need to use "
+ "@ContextConfiguration or @SpringBootTest(classes=...) with your test");
logger.info("Found @SpringBootConfiguration " + found.getName() + " for test " + mergedConfig.getTestClass()); logger.info("Found @SpringBootConfiguration " + found.getName() + " for test " + mergedConfig.getTestClass());
return merge(found, classes); return merge(found, classes);
} }
@ -258,6 +256,8 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
return ClassUtils.resolveClassName(foundClassName, testClass.getClassLoader()); return ClassUtils.resolveClassName(foundClassName, testClass.getClassLoader());
} }
Class<?> found = new AnnotatedClassFinder(SpringBootConfiguration.class).findFromClass(testClass); Class<?> found = new AnnotatedClassFinder(SpringBootConfiguration.class).findFromClass(testClass);
Assert.state(found != null, "Unable to find a @SpringBootConfiguration, you need to use "
+ "@ContextConfiguration or @SpringBootTest(classes=...) with your test");
this.aotTestAttributes.setAttribute(propertyName, found.getName()); this.aotTestAttributes.setAttribute(propertyName, found.getName());
return found; return found;
} }