|
| 1 | +/* |
| 2 | + * Copyright 2015-2017 the original author or authors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | + * accompanies this distribution and is available at |
| 7 | + * |
| 8 | + * http://www.eclipse.org/legal/epl-v10.html |
| 9 | + */ |
| 10 | + |
| 11 | +package org.junit.jupiter.engine.descriptor; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 16 | +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; |
| 17 | +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_METHOD; |
| 18 | +import static org.junit.jupiter.engine.Constants.DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME; |
| 19 | +import static org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getDefaultTestInstanceLifecycle; |
| 20 | +import static org.junit.jupiter.engine.descriptor.TestInstanceLifecycleUtils.getTestInstanceLifecycle; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.TestInstance; |
| 28 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 29 | +import org.junit.platform.commons.util.PreconditionViolationException; |
| 30 | +import org.junit.platform.engine.ConfigurationParameters; |
| 31 | + |
| 32 | +/** |
| 33 | + * Unit tests for {@link TestInstanceLifecycleUtils}. |
| 34 | + * |
| 35 | + * <p>NOTE: it doesn't make sense to unit test the JVM system property fallback |
| 36 | + * support in this test class since that feature is a concrete implementation |
| 37 | + * detail of {@code LauncherConfigurationParameters} which necessitates an |
| 38 | + * integration test via the {@code Launcher} API. |
| 39 | + * |
| 40 | + * @since 5.0 |
| 41 | + */ |
| 42 | +class TestInstanceLifecycleUtilsTests { |
| 43 | + |
| 44 | + private static final String KEY = DEFAULT_TEST_INSTANCE_LIFECYCLE_PROPERTY_NAME; |
| 45 | + |
| 46 | + @Test |
| 47 | + void getDefaultTestInstanceLifecyclePreconditions() { |
| 48 | + PreconditionViolationException exception = assertThrows(PreconditionViolationException.class, |
| 49 | + () -> getDefaultTestInstanceLifecycle(null)); |
| 50 | + assertThat(exception).hasMessage("ConfigurationParameters must not be null"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void getDefaultTestInstanceLifecycleWithNoConfigParamSet() { |
| 55 | + Lifecycle lifecycle = getDefaultTestInstanceLifecycle(mock(ConfigurationParameters.class)); |
| 56 | + assertThat(lifecycle).isEqualTo(PER_METHOD); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void getDefaultTestInstanceLifecycleWithConfigParamSet() { |
| 61 | + assertAll(// |
| 62 | + () -> assertDefaultConfigParam(null, PER_METHOD), // |
| 63 | + () -> assertDefaultConfigParam("", PER_METHOD), // |
| 64 | + () -> assertDefaultConfigParam("bogus", PER_METHOD), // |
| 65 | + () -> assertDefaultConfigParam(PER_METHOD.name(), PER_METHOD), // |
| 66 | + () -> assertDefaultConfigParam(PER_METHOD.name().toLowerCase(), PER_METHOD), // |
| 67 | + () -> assertDefaultConfigParam(" " + PER_METHOD.name() + " ", PER_METHOD), // |
| 68 | + () -> assertDefaultConfigParam(PER_CLASS.name(), PER_CLASS), // |
| 69 | + () -> assertDefaultConfigParam(PER_CLASS.name().toLowerCase(), PER_CLASS), // |
| 70 | + () -> assertDefaultConfigParam(" " + PER_CLASS.name() + " ", Lifecycle.PER_CLASS) // |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + private void assertDefaultConfigParam(String configValue, Lifecycle expected) { |
| 75 | + ConfigurationParameters configParams = mock(ConfigurationParameters.class); |
| 76 | + when(configParams.get(KEY)).thenReturn(Optional.ofNullable(configValue)); |
| 77 | + Lifecycle lifecycle = getDefaultTestInstanceLifecycle(configParams); |
| 78 | + assertThat(lifecycle).isEqualTo(expected); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void getTestInstanceLifecyclePreconditions() { |
| 83 | + PreconditionViolationException exception = assertThrows(PreconditionViolationException.class, |
| 84 | + () -> getTestInstanceLifecycle(null, mock(ConfigurationParameters.class))); |
| 85 | + assertThat(exception).hasMessage("testClass must not be null"); |
| 86 | + |
| 87 | + exception = assertThrows(PreconditionViolationException.class, |
| 88 | + () -> getTestInstanceLifecycle(getClass(), null)); |
| 89 | + assertThat(exception).hasMessage("ConfigurationParameters must not be null"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void getTestInstanceLifecycleWithNoConfigParamSet() { |
| 94 | + Lifecycle lifecycle = getTestInstanceLifecycle(getClass(), mock(ConfigurationParameters.class)); |
| 95 | + assertThat(lifecycle).isEqualTo(PER_METHOD); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void getTestInstanceLifecycleWithConfigParamSet() { |
| 100 | + ConfigurationParameters configParams = mock(ConfigurationParameters.class); |
| 101 | + when(configParams.get(KEY)).thenReturn(Optional.of(PER_CLASS.name().toLowerCase())); |
| 102 | + Lifecycle lifecycle = getTestInstanceLifecycle(getClass(), configParams); |
| 103 | + assertThat(lifecycle).isEqualTo(PER_CLASS); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void getTestInstanceLifecycleWithLocalConfigThatOverridesCustomDefaultSetViaConfigParam() { |
| 108 | + ConfigurationParameters configParams = mock(ConfigurationParameters.class); |
| 109 | + when(configParams.get(KEY)).thenReturn(Optional.of(PER_CLASS.name().toLowerCase())); |
| 110 | + Lifecycle lifecycle = getTestInstanceLifecycle(TestCase.class, configParams); |
| 111 | + assertThat(lifecycle).isEqualTo(PER_METHOD); |
| 112 | + } |
| 113 | + |
| 114 | + @TestInstance(Lifecycle.PER_METHOD) |
| 115 | + private static class TestCase { |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments