Search in sources :

Example 1 with RefreshEvent

use of io.micronaut.runtime.context.scope.refresh.RefreshEvent 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)

Example 2 with RefreshEvent

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

the class AbstractMicronautExtension method afterEach.

/**
 * Executed after each test completes.
 *
 * @param context The context
 * @throws Exception allows any exception to propagate
 */
public void afterEach(C context) throws Exception {
    if (refreshScope != null) {
        if (!oldValues.isEmpty()) {
            for (Map.Entry<String, Object> entry : oldValues.entrySet()) {
                Object value = entry.getValue();
                if (value != null) {
                    testProperties.put(entry.getKey(), value);
                } else {
                    testProperties.remove(entry.getKey());
                }
            }
            final Map<String, Object> diff = applicationContext.getEnvironment().refreshAndDiff();
            refreshScope.onRefreshEvent(new RefreshEvent(diff));
        }
    }
    oldValues.clear();
}
Also used : RefreshEvent(io.micronaut.runtime.context.scope.refresh.RefreshEvent)

Aggregations

RefreshEvent (io.micronaut.runtime.context.scope.refresh.RefreshEvent)2 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 RefreshScope (io.micronaut.runtime.context.scope.refresh.RefreshScope)1 AnnotationUtils (io.micronaut.test.annotation.AnnotationUtils)1 MicronautTestValue (io.micronaut.test.annotation.MicronautTestValue)1