Search in sources :

Example 6 with Lifecycle

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

the class TestInstanceLifecycleUtils method getDefaultTestInstanceLifecycle.

static TestInstance.Lifecycle getDefaultTestInstanceLifecycle(ConfigurationParameters configParams) {
    Preconditions.notNull(configParams, "ConfigurationParameters must not be null");
    String propertyName = DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME;
    Optional<String> optional = configParams.get(propertyName);
    String constantName = null;
    if (optional.isPresent()) {
        try {
            constantName = optional.get().trim().toUpperCase();
            Lifecycle lifecycle = TestInstance.Lifecycle.valueOf(constantName);
            logger.info(() -> String.format("Using default test instance lifecycle mode '%s' set via the '%s' configuration parameter.", lifecycle, propertyName));
            return lifecycle;
        } catch (Exception ex) {
            // local copy necessary for use in lambda expression
            String constant = constantName;
            logger.warn(() -> String.format("Invalid test instance lifecycle mode '%s' set via the '%s' configuration parameter. " + "Falling back to %s lifecycle semantics.", constant, propertyName, Lifecycle.PER_METHOD.name()));
        }
    }
    return Lifecycle.PER_METHOD;
}
Also used : Lifecycle(org.junit.jupiter.api.TestInstance.Lifecycle)

Example 7 with Lifecycle

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

the class TestInstanceLifecycleUtilsTests method assertDefaultConfigParam.

private void assertDefaultConfigParam(String configValue, Lifecycle expected) {
    ConfigurationParameters configParams = mock(ConfigurationParameters.class);
    when(configParams.get(KEY)).thenReturn(Optional.ofNullable(configValue));
    Lifecycle lifecycle = getDefaultTestInstanceLifecycle(configParams);
    assertThat(lifecycle).isEqualTo(expected);
}
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)

Example 8 with Lifecycle

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

the class TestInstanceLifecycleUtilsTests method getTestInstanceLifecycleWithLocalConfigThatOverridesCustomDefaultSetViaConfigParam.

@Test
void getTestInstanceLifecycleWithLocalConfigThatOverridesCustomDefaultSetViaConfigParam() {
    ConfigurationParameters configParams = mock(ConfigurationParameters.class);
    when(configParams.get(KEY)).thenReturn(Optional.of(PER_CLASS.name().toLowerCase()));
    Lifecycle lifecycle = getTestInstanceLifecycle(TestCase.class, configParams);
    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 9 with Lifecycle

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

the class TestInstanceLifecycleUtilsTests method getDefaultTestInstanceLifecycleWithNoConfigParamSet.

@Test
void getDefaultTestInstanceLifecycleWithNoConfigParamSet() {
    Lifecycle lifecycle = getDefaultTestInstanceLifecycle(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 10 with Lifecycle

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

the class TestInstanceLifecycleUtilsTests method getTestInstanceLifecycleFromSpecializedClassWithNoConfigParamSet.

@Test
void getTestInstanceLifecycleFromSpecializedClassWithNoConfigParamSet() {
    Class<?> testClass = SpecializedTestCase.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)

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