use of org.junit.jupiter.api.extension.ExtensionContext in project curiostack by curioswitch.
the class SnapshotAssertions method assertSnapshotMatches.
public static void assertSnapshotMatches(Object obj) {
checkArgument(OBJECT_MAPPER.canSerialize(obj.getClass()), "obj must be JSON serializable");
ExtensionContext ctx = SnapshotExtension.CURRENT_CONTEXT.get();
checkNotNull(ctx, "Either SnapshotExtension is not registered or the current assertion " + "was made on a different thread than the test thread.");
Store store = ctx.getStore(Namespace.create(SnapshotAssertions.class, ctx.getUniqueId()));
int currentIndex = store.getOrComputeIfAbsent(AssertionIndexKey.INSTANCE, (unused) -> 0, Integer.class);
store.put(AssertionIndexKey.INSTANCE, currentIndex + 1);
String prefix = ctx.getRequiredTestClass().getCanonicalName().substring(ctx.getRequiredTestClass().getPackageName().length() + 1);
String snapshot = SnapshotManager.INSTANCE.getTestSnapshot(getTopLevelClass(ctx), prefix + '.' + ctx.getRequiredTestMethod().getName(), currentIndex, obj);
final Object snapshotObj;
try {
snapshotObj = OBJECT_MAPPER.readValue(snapshot, obj.getClass());
} catch (IOException e) {
throw new AssertionError("Snapshot does not match type of asserted object.");
}
if (obj.equals(snapshotObj)) {
return;
}
final String serialized;
try {
serialized = OBJECT_MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new UncheckedIOException("Could not serialize obj, can't happen.", e);
}
if (serialized.equals(snapshot)) {
return;
}
// better IDE integration.
try {
assertEquals(snapshot, serialized);
} catch (AssertionFailedError e) {
throw new AssertionFailedError("Snapshot doesn't match. If the diff is expected, run with updateSnapshots set. " + e.getMessage(), e.getExpected(), e.getActual());
}
}
use of org.junit.jupiter.api.extension.ExtensionContext in project core-ng-project by neowu.
the class IntegrationExtension method postProcessTestInstance.
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
ExtensionContext.Store store = context.getRoot().getStore(ExtensionContext.Namespace.GLOBAL);
Class<?> testClass = context.getRequiredTestClass();
TestBeanFactory beanFactory = store.getOrComputeIfAbsent(TestBeanFactory.class, key -> createTestBeanFactory(testClass, store), TestBeanFactory.class);
beanFactory.inject(testInstance);
}
Aggregations