Search in sources :

Example 1 with RefreshScope

use of io.micronaut.runtime.context.scope.refresh.RefreshScope in project micronaut-test by micronaut-projects.

the class AbstractMicronautExtension method beforeEach.

/**
 * To be called by the different implementations before each test method.
 *
 * @param context The test context
 * @param testInstance The test instance
 * @param method The test method
 * @param propertyAnnotations The {@code @Property} annotations found in the test method, if any
 */
protected void beforeEach(C context, @Nullable Object testInstance, @Nullable AnnotatedElement method, List<Property> propertyAnnotations) {
    int testCount = (int) testProperties.compute("micronaut.test.count", (k, oldCount) -> (int) (oldCount != null ? oldCount : 0) + 1);
    if (method != null) {
        if (propertyAnnotations != null && !propertyAnnotations.isEmpty()) {
            for (Property property : propertyAnnotations) {
                final String name = property.name();
                oldValues.put(name, testProperties.put(name, property.value()));
            }
        } else {
            oldValues.forEach((k, v) -> testProperties.put(k, v));
        }
        if (testAnnotationValue.rebuildContext() && testCount > 1) {
            stopEmbeddedApplication();
            if (applicationContext.isRunning()) {
                applicationContext.stop();
            }
            applicationContext = builder.build();
            startApplicationContext();
            startEmbeddedApplication();
        } else if (!oldValues.isEmpty()) {
            final Map<String, Object> diff = applicationContext.getEnvironment().refreshAndDiff();
            refreshScope.onRefreshEvent(new RefreshEvent(diff));
        }
    }
    if (testInstance != null) {
        if (applicationContext != null) {
            if (refreshScope != null) {
                refreshScope.onRefreshEvent(new RefreshEvent(Collections.singletonMap(TestActiveCondition.ACTIVE_MOCKS, "changed")));
            }
            applicationContext.inject(testInstance);
            alignMocks(context, testInstance);
        }
    }
}
Also used : java.util(java.util) TestContext(io.micronaut.test.context.TestContext) RefreshEvent(io.micronaut.runtime.context.scope.refresh.RefreshEvent) ArrayUtils(io.micronaut.core.util.ArrayUtils) SoftServiceLoader(io.micronaut.core.io.service.SoftServiceLoader) ApplicationContext(io.micronaut.context.ApplicationContext) TestExecutionListener(io.micronaut.test.context.TestExecutionListener) InstantiationUtils(io.micronaut.core.reflect.InstantiationUtils) Nullable(io.micronaut.core.annotation.Nullable) AnnotationUtils(io.micronaut.test.annotation.AnnotationUtils) NameUtils(io.micronaut.core.naming.NameUtils) Property(io.micronaut.context.annotation.Property) ResourceResolver(io.micronaut.core.io.ResourceResolver) ClassUtils(io.micronaut.core.reflect.ClassUtils) ApplicationContextBuilder(io.micronaut.context.ApplicationContextBuilder) PropertySource(io.micronaut.context.env.PropertySource) PropertySourceLoader(io.micronaut.context.env.PropertySourceLoader) TestActiveCondition(io.micronaut.test.condition.TestActiveCondition) ServiceDefinition(io.micronaut.core.io.service.ServiceDefinition) IOException(java.io.IOException) EmbeddedApplication(io.micronaut.runtime.EmbeddedApplication) StringUtils(io.micronaut.core.util.StringUtils) MicronautTestValue(io.micronaut.test.annotation.MicronautTestValue) RefreshScope(io.micronaut.runtime.context.scope.refresh.RefreshScope) BeanDefinition(io.micronaut.inject.BeanDefinition) TestPropertyProvider(io.micronaut.test.support.TestPropertyProvider) InputStream(java.io.InputStream) AnnotatedElement(java.lang.reflect.AnnotatedElement) RefreshEvent(io.micronaut.runtime.context.scope.refresh.RefreshEvent) Property(io.micronaut.context.annotation.Property)

Aggregations

ApplicationContext (io.micronaut.context.ApplicationContext)1 ApplicationContextBuilder (io.micronaut.context.ApplicationContextBuilder)1 Property (io.micronaut.context.annotation.Property)1 PropertySource (io.micronaut.context.env.PropertySource)1 PropertySourceLoader (io.micronaut.context.env.PropertySourceLoader)1 Nullable (io.micronaut.core.annotation.Nullable)1 ResourceResolver (io.micronaut.core.io.ResourceResolver)1 ServiceDefinition (io.micronaut.core.io.service.ServiceDefinition)1 SoftServiceLoader (io.micronaut.core.io.service.SoftServiceLoader)1 NameUtils (io.micronaut.core.naming.NameUtils)1 ClassUtils (io.micronaut.core.reflect.ClassUtils)1 InstantiationUtils (io.micronaut.core.reflect.InstantiationUtils)1 ArrayUtils (io.micronaut.core.util.ArrayUtils)1 StringUtils (io.micronaut.core.util.StringUtils)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 EmbeddedApplication (io.micronaut.runtime.EmbeddedApplication)1 RefreshEvent (io.micronaut.runtime.context.scope.refresh.RefreshEvent)1 RefreshScope (io.micronaut.runtime.context.scope.refresh.RefreshScope)1 AnnotationUtils (io.micronaut.test.annotation.AnnotationUtils)1 MicronautTestValue (io.micronaut.test.annotation.MicronautTestValue)1