/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getLocations() locations}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasLocations()) { String msg = String.format("Test class [%s] has been configured with @ContextConfiguration's 'locations' " + "(or 'value') attribute %s, but %s does not support resource locations.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getLocations()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Returns a {@link WebMergedContextConfiguration} if the test class in the * supplied {@code MergedContextConfiguration} is annotated with * {@link WebAppConfiguration @WebAppConfiguration} and otherwise returns * the supplied instance unmodified. */ @Override protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) { WebAppConfiguration webAppConfiguration = AnnotatedElementUtils.findMergedAnnotation(mergedConfig.getTestClass(), WebAppConfiguration.class); if (webAppConfiguration != null) { return new WebMergedContextConfiguration(mergedConfig, webAppConfiguration.value()); } else { return mergedConfig; } }
void assertMergedConfig( MergedContextConfiguration mergedConfig, Class<?> expectedTestClass, String[] expectedLocations, Class<?>[] expectedClasses, Set<Class<? extends ApplicationContextInitializer<?>>> expectedInitializerClasses, Class<? extends ContextLoader> expectedContextLoaderClass) { assertNotNull(mergedConfig); assertEquals(expectedTestClass, mergedConfig.getTestClass()); assertNotNull(mergedConfig.getLocations()); assertArrayEquals(expectedLocations, mergedConfig.getLocations()); assertNotNull(mergedConfig.getClasses()); assertArrayEquals(expectedClasses, mergedConfig.getClasses()); assertNotNull(mergedConfig.getActiveProfiles()); if (expectedContextLoaderClass == null) { assertNull(mergedConfig.getContextLoader()); } else { assertEquals(expectedContextLoaderClass, mergedConfig.getContextLoader().getClass()); } assertNotNull(mergedConfig.getContextInitializerClasses()); assertEquals(expectedInitializerClasses, mergedConfig.getContextInitializerClasses()); }
import org.kubek2k.springockito.annotations.internal.Loader; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.GenericApplicationContext; import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.support.GenericXmlContextLoader; public class PatchedSpringockitoContextLoader extends GenericXmlContextLoader { private final Loader loader = new Loader(); @Override protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { super.prepareContext(context, mergedConfig); this.loader.defineMocksAndSpies(mergedConfig.getTestClass()); } @Override protected void customizeContext(GenericApplicationContext context) { super.customizeContext(context); this.loader.registerMocksAndSpies(context); } }
private boolean isFromConfiguration(MergedContextConfiguration candidateConfig, ContextConfiguration configuration) { ContextConfigurationAttributes attributes = new ContextConfigurationAttributes( candidateConfig.getTestClass(), configuration); Set<Class<?>> configurationClasses = new HashSet<>( Arrays.asList(attributes.getClasses())); for (Class<?> candidate : candidateConfig.getClasses()) { if (configurationClasses.contains(candidate)) { return true; } } return false; }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getClasses() classes}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
/** * Ensure that the supplied {@link MergedContextConfiguration} does not * contain {@link MergedContextConfiguration#getLocations() locations}. * @since 4.0.4 * @see AbstractGenericContextLoader#validateMergedContextConfiguration */ @Override protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) { if (mergedConfig.hasLocations()) { String msg = String.format("Test class [%s] has been configured with @ContextConfiguration's 'locations' " + "(or 'value') attribute %s, but %s does not support resource locations.", mergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(mergedConfig.getLocations()), getClass().getSimpleName()); logger.error(msg); throw new IllegalStateException(msg); } }
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); }
public TinyMergedContextConfiguration(ContextLoader contextLoader, CacheAwareContextLoaderDelegate contextLoaderDelegate, MergedContextConfiguration mergedContextConfiguration, MergedContextConfiguration parentConfiguration) { super(mergedContextConfiguration.getTestClass(), mergedContextConfiguration.getLocations(), mergedContextConfiguration.getClasses(), mergedContextConfiguration.getActiveProfiles(), contextLoader); this.mergedContextConfiguration = mergedContextConfiguration; this.contextLoaderDelegate = contextLoaderDelegate; this.parentConfiguration = parentConfiguration; }
private boolean isEmbeddedWebEnvironment(MergedContextConfiguration config) { SpringBootTest annotation = AnnotatedElementUtils .findMergedAnnotation(config.getTestClass(), SpringBootTest.class); if (annotation != null && annotation.webEnvironment().isEmbedded()) { return true; } return false; }
/** * Returns a {@link WebMergedContextConfiguration} if the test class in the * supplied {@code MergedContextConfiguration} is annotated with * {@link WebAppConfiguration @WebAppConfiguration} and otherwise returns * the supplied instance unmodified. */ @Override protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) { WebAppConfiguration webAppConfiguration = AnnotatedElementUtils.findMergedAnnotation(mergedConfig.getTestClass(), WebAppConfiguration.class); if (webAppConfiguration != null) { return new WebMergedContextConfiguration(mergedConfig, webAppConfiguration.value()); } else { return mergedConfig; } }
private boolean isWebEnvironmentSupported(MergedContextConfiguration mergedConfig) { Class<?> testClass = mergedConfig.getTestClass(); ContextHierarchy hierarchy = AnnotationUtils.getAnnotation(testClass, ContextHierarchy.class); if (hierarchy == null || hierarchy.value().length == 0) { return true; } ContextConfiguration[] configurations = hierarchy.value(); return isFromConfiguration(mergedConfig, configurations[configurations.length - 1]); }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { SpringBootTest annotation = AnnotatedElementUtils .getMergedAnnotation(mergedConfig.getTestClass(), SpringBootTest.class); if (annotation.webEnvironment().isEmbedded()) { registerWebTestClient(context); } }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedContextConfiguration) { SpringBootTest annotation = AnnotatedElementUtils.getMergedAnnotation( mergedContextConfiguration.getTestClass(), SpringBootTest.class); if (annotation.webEnvironment().isEmbedded()) { registerTestRestTemplate(context); } }
@Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { context.addBeanFactoryPostProcessor(new EnvironmentPostProcessor(context.getEnvironment())); Class<?> testClass = mergedConfig.getTestClass(); FlywayTest[] flywayAnnotations = findFlywayTestAnnotations(testClass); BeanDefinitionRegistry registry = getBeanDefinitionRegistry(context); RootBeanDefinition registrarDefinition = new RootBeanDefinition(); registrarDefinition.setBeanClass(PreloadableEmbeddedPostgresRegistrar.class); registrarDefinition.getConstructorArgumentValues() .addIndexedArgumentValue(0, databaseAnnotation); registrarDefinition.getConstructorArgumentValues() .addIndexedArgumentValue(1, flywayAnnotations); registry.registerBeanDefinition("preloadableEmbeddedPostgresRegistrar", registrarDefinition); }