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);
}
}
}
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();
}
Aggregations