use of io.micronaut.test.extensions.spock.annotation.MicronautTest in project micronaut-test by micronaut-projects.
the class MicronautSpockExtension method visitSpecAnnotation.
@Override
public void visitSpecAnnotation(T annotation, SpecInfo spec) {
spec.getAllFeatures().forEach(feature -> {
feature.addInterceptor(invocation -> {
try {
beforeTestMethod(buildContext(invocation, null));
invocation.proceed();
} finally {
afterTestMethod(buildContext(invocation, null));
}
});
feature.getFeatureMethod().addInterceptor(invocation -> {
try {
beforeTestExecution(buildContext(invocation, null));
invocation.proceed();
afterTestExecution(buildContext(invocation, null));
} catch (Throwable e) {
afterTestExecution(buildContext(invocation, e));
throw e;
}
});
});
spec.addSetupSpecInterceptor(invocation -> {
MicronautTest micronautTest = spec.getAnnotation(MicronautTest.class);
MicronautTestValue micronautTestValue = buildValueObject(micronautTest);
beforeClass(invocation, spec.getReflection(), micronautTestValue);
if (specDefinition == null) {
if (!isTestSuiteBeanPresent(spec.getReflection())) {
throw new InvalidSpecException(MISCONFIGURED_MESSAGE);
} else {
final List<FeatureInfo> features = invocation.getSpec().getFeatures();
for (FeatureInfo feature : features) {
feature.setSkipped(true);
}
}
} else {
List<FieldInfo> fields = spec.getAllFields();
for (FieldInfo field : fields) {
if (field.isShared() && field.getAnnotation(Inject.class) != null) {
applicationContext.inject(invocation.getSharedInstance());
break;
}
}
}
beforeTestClass(buildContext(invocation, null));
invocation.proceed();
});
spec.addCleanupSpecInterceptor(invocation -> {
afterTestClass(buildContext(invocation, null));
afterClass(invocation);
invocation.proceed();
singletonMocks.clear();
});
spec.addSetupInterceptor(invocation -> {
final Object instance = invocation.getInstance();
final Method method = invocation.getFeature().getFeatureMethod().getReflection();
List<Property> propertyAnnotations = Arrays.asList(method.getAnnotationsByType(Property.class));
beforeEach(invocation, instance, method, propertyAnnotations);
for (Object mock : creatableMocks) {
mockUtil.attachMock(mock, (Specification) instance);
}
for (Object mock : singletonMocks) {
mockUtil.attachMock(mock, (Specification) instance);
}
try {
beforeSetupTest(buildContext(invocation, null));
invocation.proceed();
} finally {
afterSetupTest(buildContext(invocation, null));
}
});
spec.addCleanupInterceptor(invocation -> {
for (Object mock : creatableMocks) {
mockUtil.detachMock(mock);
}
for (Object mock : singletonMocks) {
mockUtil.detachMock(mock);
}
creatableMocks.clear();
afterEach(invocation);
try {
beforeCleanupTest(buildContext(invocation, null));
invocation.proceed();
} finally {
afterCleanupTest(buildContext(invocation, null));
}
});
}
Aggregations