-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for JUnit 5 @nested test classes
Test with relevant combinations of Lifecycle PER_METHOD and PER_CLASS Signed-off-by: Jonas Höf <jonas.hoef@tngtech.com>
- Loading branch information
Showing
3 changed files
with
338 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...5/src/test/java/com/tngtech/valueprovider/nested/LifecycleMainPerClassNestedDemoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.tngtech.valueprovider.nested; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.tngtech.valueprovider.ValueProvider; | ||
import com.tngtech.valueprovider.ValueProviderAsserter; | ||
import com.tngtech.valueprovider.ValueProviderExtension; | ||
import org.junit.jupiter.api.*; | ||
import org.junit.jupiter.api.MethodOrderer.MethodName; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static com.tngtech.valueprovider.JUnit5Tests.ensureDefinedFactoryState; | ||
import static com.tngtech.valueprovider.ValueProviderFactory.createRandomValueProvider; | ||
import static java.util.Arrays.asList; | ||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; | ||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_METHOD; | ||
|
||
@TestInstance(PER_CLASS) | ||
@DisplayName("Main test class, Lifecycle PER_CLASS") | ||
@ExtendWith(ValueProviderExtension.class) | ||
class LifecycleMainPerClassNestedDemoTest { | ||
private static final ValueProvider classRandom; | ||
|
||
static { | ||
ensureDefinedFactoryState(); | ||
classRandom = createRandomValueProvider(); | ||
} | ||
|
||
private final List<ValueProvider> randomsOfPreviousTestMethods = new ArrayList<>(); | ||
private final ValueProvider mainInstanceRandom = createRandomValueProvider(); | ||
private ValueProvider mainBeforeAllRandom; | ||
private ValueProvider mainBeforeEachRandom; | ||
|
||
@BeforeAll | ||
void mainBeforeAll() { | ||
mainBeforeAllRandom = createRandomValueProvider(); | ||
} | ||
|
||
@BeforeEach | ||
void mainBeforeEach() { | ||
mainBeforeEachRandom = createRandomValueProvider(); | ||
} | ||
|
||
@Test | ||
void should_ensure_reproducible_ValueProvider_creation_in_main_class() { | ||
verifyReproducibleValueProviderCreationAndAdd(mainBeforeEachRandom, createRandomValueProvider()); | ||
} | ||
|
||
@Test | ||
void should_ensure_proper_separation_of_test_class_and_test_method_cycles_in_main_class() { | ||
verifyReproducibleValueProviderCreationAndAdd(mainBeforeEachRandom, createRandomValueProvider()); | ||
} | ||
|
||
@Nested | ||
@TestInstance(PER_METHOD) | ||
@DisplayName("Nested test class, Lifecycle PER_METHOD") | ||
class LifecycleNestedPerMethod { | ||
private final ValueProvider nestedInstanceRandom = createRandomValueProvider(); | ||
private ValueProvider nestedBeforeEachRandom; | ||
|
||
@BeforeEach | ||
void nestedBeforeEach() { | ||
nestedBeforeEachRandom = createRandomValueProvider(); | ||
} | ||
|
||
@Test | ||
void should_ensure_reproducible_ValueProvider_creation_in_nested_class() { | ||
verifyReproducibleValueProviderCreationAndAdd(nestedInstanceRandom, | ||
mainBeforeEachRandom, nestedBeforeEachRandom, | ||
createRandomValueProvider()); | ||
} | ||
|
||
@Test | ||
void should_ensure_proper_separation_of_test_class_and_test_method_cycles_in_nested_class() { | ||
verifyReproducibleValueProviderCreationAndAdd(nestedInstanceRandom, | ||
mainBeforeEachRandom, nestedBeforeEachRandom, | ||
createRandomValueProvider()); | ||
} | ||
} | ||
|
||
@Nested | ||
@TestInstance(PER_CLASS) | ||
@TestMethodOrder(MethodName.class) | ||
@DisplayName("Nested test class, Lifecycle PER_CLASS") | ||
class LifecycleNestedPerClass { | ||
private final ValueProvider nestedInstanceRandom = createRandomValueProvider(); | ||
private ValueProvider nestedBeforeAllRandom; | ||
private ValueProvider nestedBeforeEachRandom; | ||
|
||
@BeforeAll | ||
void nestedBeforeAll() { | ||
nestedBeforeAllRandom = createRandomValueProvider(); | ||
} | ||
|
||
@BeforeEach | ||
void nestedBeforeEach() { | ||
nestedBeforeEachRandom = createRandomValueProvider(); | ||
} | ||
|
||
@Test | ||
void a_should_ensure_reproducible_ValueProvider_creation_in_nested_class() { | ||
verifyReproducibleValueProviderCreationAndAdd(nestedInstanceRandom, nestedBeforeAllRandom, | ||
mainBeforeEachRandom, nestedBeforeEachRandom, | ||
createRandomValueProvider()); | ||
} | ||
|
||
@Test | ||
void b_should_ensure_proper_separation_of_test_class_and_test_method_cycles_in_nested_class() { | ||
verifyReproducibleValueProviderCreationAndAdd(mainBeforeEachRandom, nestedBeforeEachRandom, | ||
createRandomValueProvider()); | ||
} | ||
} | ||
|
||
private void verifyReproducibleValueProviderCreationAndAdd(ValueProvider... additionalTestMethodRandomValues) { | ||
List<ValueProvider> additionalTestMethodRamdomValuesList = asList(additionalTestMethodRandomValues); | ||
new ValueProviderAsserter() | ||
.addExpectedTestClassRandomValues(classRandom) | ||
.addExpectedTestMethodRandomValues(mainInstanceRandom, mainBeforeAllRandom) | ||
.addExpectedTestMethodRandomValues(randomsOfPreviousTestMethods) | ||
.addExpectedTestMethodRandomValues(additionalTestMethodRamdomValuesList) | ||
.assertAllTestClassRandomValues() | ||
.assertAllTestMethodRandomValues() | ||
.assertAllSuffixes(); | ||
randomsOfPreviousTestMethods.addAll(additionalTestMethodRamdomValuesList); | ||
} | ||
} |
Oops, something went wrong.