Search in sources :

Example 1 with Lifecycle

use of org.junit.jupiter.api.TestInstance.Lifecycle in project junit5 by junit-team.

the class TestInstanceLifecycleUtilsTests method getTestInstanceLifecycleFromMetaAnnotationWithNoConfigParamSet.

@Test
void getTestInstanceLifecycleFromMetaAnnotationWithNoConfigParamSet() {
    Class<?> testClass = BaseMetaAnnotatedTestCase.class;
    Lifecycle lifecycle = getTestInstanceLifecycle(testClass, mock(ConfigurationParameters.class));
    assertThat(lifecycle).isEqualTo(PER_CLASS);
}
Also used : Lifecycle(org.junit.jupiter.api.TestInstance.Lifecycle) TestInstanceLifecycleUtils.getTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle) TestInstanceLifecycleUtils.getDefaultTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getDefaultTestInstanceLifecycle) ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) Test(org.junit.jupiter.api.Test)

Example 2 with Lifecycle

use of org.junit.jupiter.api.TestInstance.Lifecycle in project junit5 by junit-team.

the class TestInstanceLifecycleUtilsTests method getTestInstanceLifecycleWithConfigParamSet.

@Test
void getTestInstanceLifecycleWithConfigParamSet() {
    ConfigurationParameters configParams = mock(ConfigurationParameters.class);
    when(configParams.get(KEY)).thenReturn(Optional.of(PER_CLASS.name().toLowerCase()));
    Lifecycle lifecycle = getTestInstanceLifecycle(getClass(), configParams);
    assertThat(lifecycle).isEqualTo(PER_CLASS);
}
Also used : Lifecycle(org.junit.jupiter.api.TestInstance.Lifecycle) TestInstanceLifecycleUtils.getTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle) TestInstanceLifecycleUtils.getDefaultTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getDefaultTestInstanceLifecycle) ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) Test(org.junit.jupiter.api.Test)

Example 3 with Lifecycle

use of org.junit.jupiter.api.TestInstance.Lifecycle in project junit5 by junit-team.

the class TestInstanceLifecycleUtilsTests method getTestInstanceLifecycleWithNoConfigParamSet.

@Test
void getTestInstanceLifecycleWithNoConfigParamSet() {
    Lifecycle lifecycle = getTestInstanceLifecycle(getClass(), mock(ConfigurationParameters.class));
    assertThat(lifecycle).isEqualTo(PER_METHOD);
}
Also used : Lifecycle(org.junit.jupiter.api.TestInstance.Lifecycle) TestInstanceLifecycleUtils.getTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle) TestInstanceLifecycleUtils.getDefaultTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getDefaultTestInstanceLifecycle) ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) Test(org.junit.jupiter.api.Test)

Example 4 with Lifecycle

use of org.junit.jupiter.api.TestInstance.Lifecycle in project junit5 by junit-team.

the class ClassTestDescriptor method prepare.

// --- Node ----------------------------------------------------------------
@Override
public JupiterEngineExecutionContext prepare(JupiterEngineExecutionContext context) {
    ExtensionRegistry registry = populateNewExtensionRegistryFromExtendWithAnnotation(context.getExtensionRegistry(), this.testClass);
    // Register extensions from static fields here, at the class level but
    // after extensions registered via @ExtendWith.
    registerExtensionsFromFields(registry, this.testClass, null);
    registerBeforeEachMethodAdapters(registry);
    registerAfterEachMethodAdapters(registry);
    Lifecycle lifecycle = getTestInstanceLifecycle(this.testClass, context.getConfigurationParameters());
    ThrowableCollector throwableCollector = new ThrowableCollector();
    ClassExtensionContext extensionContext = new ClassExtensionContext(context.getExtensionContext(), context.getExecutionListener(), this, lifecycle, context.getConfigurationParameters(), throwableCollector);
    this.beforeAllMethods = findBeforeAllMethods(this.testClass, lifecycle == Lifecycle.PER_METHOD);
    this.afterAllMethods = findAfterAllMethods(this.testClass, lifecycle == Lifecycle.PER_METHOD);
    // @formatter:off
    return context.extend().withTestInstanceProvider(testInstanceProvider(context, registry, extensionContext)).withExtensionRegistry(registry).withExtensionContext(extensionContext).withThrowableCollector(throwableCollector).build();
// @formatter:on
}
Also used : TestInstanceLifecycleUtils.getTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle) Lifecycle(org.junit.jupiter.api.TestInstance.Lifecycle) ExtensionRegistry(org.junit.jupiter.engine.extension.ExtensionRegistry) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Example 5 with Lifecycle

use of org.junit.jupiter.api.TestInstance.Lifecycle in project junit5 by junit-team.

the class ClassTestDescriptor method before.

@Override
public JupiterEngineExecutionContext before(JupiterEngineExecutionContext context) throws Exception {
    Lifecycle lifecycle = context.getExtensionContext().getTestInstanceLifecycle().orElse(Lifecycle.PER_METHOD);
    if (lifecycle == Lifecycle.PER_CLASS) {
        // Eagerly load test instance for BeforeAllCallbacks, if necessary,
        // and store the instance in the ExtensionContext.
        ClassExtensionContext extensionContext = (ClassExtensionContext) context.getExtensionContext();
        extensionContext.setTestInstance(context.getTestInstanceProvider().getTestInstance(Optional.empty()));
    }
    ThrowableCollector throwableCollector = context.getThrowableCollector();
    invokeBeforeAllCallbacks(context);
    if (throwableCollector.isEmpty()) {
        context.beforeAllMethodsExecuted(true);
        invokeBeforeAllMethods(context);
    }
    throwableCollector.assertEmpty();
    return context;
}
Also used : TestInstanceLifecycleUtils.getTestInstanceLifecycle(org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle) Lifecycle(org.junit.jupiter.api.TestInstance.Lifecycle) ThrowableCollector(org.junit.jupiter.engine.execution.ThrowableCollector)

Aggregations

Lifecycle (org.junit.jupiter.api.TestInstance.Lifecycle)10 TestInstanceLifecycleUtils.getTestInstanceLifecycle (org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle)9 TestInstanceLifecycleUtils.getDefaultTestInstanceLifecycle (org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getDefaultTestInstanceLifecycle)7 ConfigurationParameters (org.junit.platform.engine.ConfigurationParameters)7 Test (org.junit.jupiter.api.Test)6 ThrowableCollector (org.junit.jupiter.engine.execution.ThrowableCollector)2 ExtensionRegistry (org.junit.jupiter.engine.extension.ExtensionRegistry)1