Search in sources :

Example 36 with ExtensionContext

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());
    }
}
Also used : ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Store(org.junit.jupiter.api.extension.ExtensionContext.Store) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) AssertionFailedError(org.opentest4j.AssertionFailedError) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 37 with ExtensionContext

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);
}
Also used : TestBeanFactory(core.framework.test.inject.TestBeanFactory) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext)

Aggregations

ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)37 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)13 Method (java.lang.reflect.Method)11 ThrowableCollector (org.junit.jupiter.engine.execution.ThrowableCollector)11 Test (org.junit.jupiter.api.Test)10 Optional (java.util.Optional)7 UniqueId (org.junit.platform.engine.UniqueId)5 List (java.util.List)4 Stream (java.util.stream.Stream)4 API (org.apiguardian.api.API)4 INTERNAL (org.apiguardian.api.API.Status.INTERNAL)4 Executable (org.junit.jupiter.api.function.Executable)4 JupiterEngineExecutionContext (org.junit.jupiter.engine.execution.JupiterEngineExecutionContext)4 Preconditions (org.junit.platform.commons.util.Preconditions)4 Arrays (java.util.Arrays)3 AfterEachCallback (org.junit.jupiter.api.extension.AfterEachCallback)3 BeforeEachCallback (org.junit.jupiter.api.extension.BeforeEachCallback)3 TestTemplateInvocationContext (org.junit.jupiter.api.extension.TestTemplateInvocationContext)3 ClassExtensionContext (org.junit.jupiter.engine.descriptor.ClassExtensionContext)3 JUnitException (org.junit.platform.commons.JUnitException)3