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