/** * Find the first {@link Class} that is annotated with the target annotation, starting * from the package defined by the given {@code source} up to the root. * @param source the source class to use to initiate the search * @return the first {@link Class} annotated with the target annotation within the * hierarchy defined by the given {@code source} or {@code null} if none is found. */ public Class<?> findFromClass(Class<?> source) { Assert.notNull(source, "Source must not be null"); return findFromPackage(ClassUtils.getPackageName(source)); }
/** * Find the first {@link Class} that is annotated with the target annotation, starting * from the package defined by the given {@code source} up to the root. * @param source the source package to use to initiate the search * @return the first {@link Class} annotated with the target annotation within the * hierarchy defined by the given {@code source} or {@code null} if none is found. */ public Class<?> findFromPackage(String source) { Assert.notNull(source, "Source must not be null"); Class<?> configuration = cache.get(source); if (configuration == null) { configuration = scanPackage(source); cache.put(source, configuration); } return configuration; }
protected Class<?>[] getOrFindConfigurationClasses( MergedContextConfiguration mergedConfig) { Class<?>[] classes = mergedConfig.getClasses(); if (containsNonTestComponent(classes) || mergedConfig.hasLocations()) { return classes; } Class<?> found = new AnnotatedClassFinder(SpringBootConfiguration.class) .findFromClass(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()); return merge(found, classes); }
private Class<?> scanPackage(String source) { while (!source.isEmpty()) { Set<BeanDefinition> components = this.scanner.findCandidateComponents(source); if (!components.isEmpty()) { Assert.state(components.size() == 1, () -> "Found multiple @" + this.annotationType.getSimpleName() + " annotated classes " + components); return ClassUtils.resolveClassName( components.iterator().next().getBeanClassName(), null); } source = getParentPackage(source); } return null; }