-
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 TestInstance.Lifecycle.PER_CLASS
Signed-off-by: Jonas Höf <jonas.hoef@tngtech.com>
- Loading branch information
Showing
5 changed files
with
334 additions
and
13 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
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
37 changes: 37 additions & 0 deletions
37
.../src/test/java/com/tngtech/valueprovider/LifecyclePerClassAllDisabledTestMethodsTest.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,37 @@ | ||
package com.tngtech.valueprovider; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.TestInstance.Lifecycle; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; | ||
|
||
/** | ||
* No need for different sequences of enabled/disabled test methods | ||
* like for {@link Lifecycle#PER_CLASS}, | ||
* as test method cycle is started once in intercepted constructor, | ||
* and only finished after last test method. | ||
* | ||
* @see DisabledEnabledDisabledTestMethodsTest | ||
* @see EnabledDisabledEnabledTestMethodsTest | ||
*/ | ||
@TestInstance(PER_CLASS) | ||
@ExtendWith(ValueProviderExtension.class) | ||
class LifecyclePerClassAllDisabledTestMethodsTest { | ||
@Disabled | ||
@Test | ||
void a_test_disabled() { | ||
} | ||
|
||
@Disabled | ||
@Test | ||
void b_test_disabled() { | ||
} | ||
|
||
@Disabled | ||
@Test | ||
void c_test_disabled() { | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
junit5/src/test/java/com/tngtech/valueprovider/LifecyclePerClassDataProviderDemoTest.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,132 @@ | ||
package com.tngtech.valueprovider; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.tngtech.junit.dataprovider.DataProvider; | ||
import com.tngtech.junit.dataprovider.UseDataProvider; | ||
import com.tngtech.junit.dataprovider.UseDataProviderExtension; | ||
import org.junit.jupiter.api.*; | ||
import org.junit.jupiter.api.MethodOrderer.MethodName; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static com.tngtech.junit.dataprovider.DataProviders.$; | ||
import static com.tngtech.junit.dataprovider.DataProviders.$$; | ||
import static com.tngtech.valueprovider.JUnit5Tests.asListWithoutNulls; | ||
import static com.tngtech.valueprovider.JUnit5Tests.ensureDefinedFactoryState; | ||
import static com.tngtech.valueprovider.ValueProviderFactory.createRandomValueProvider; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; | ||
|
||
@TestInstance(PER_CLASS) | ||
@TestMethodOrder(MethodName.class) | ||
@ExtendWith({ValueProviderExtension.class, UseDataProviderExtension.class}) | ||
class LifecyclePerClassDataProviderDemoTest { | ||
private static final Logger logger = LoggerFactory.getLogger(LifecyclePerClassDataProviderDemoTest.class); | ||
private static final ValueProvider classRandom1; | ||
private static final ValueProvider classRandom2; | ||
|
||
static { | ||
logger.debug("{}: static initialization", LifecyclePerClassDataProviderDemoTest.class.getSimpleName()); | ||
ensureDefinedFactoryState(); | ||
classRandom1 = createRandomValueProvider(); | ||
classRandom2 = createRandomValueProvider(); | ||
} | ||
|
||
private final ValueProvider instanceRandom = createRandomValueProvider(); | ||
private ValueProvider beforeAllRandom; | ||
private ValueProvider dataProviderRandom; | ||
private ValueProvider beforeEachRandom; | ||
private ValueProvider methodRandom; | ||
|
||
private final List<ValueProvider> randomsOfPreviousTestMethods = new ArrayList<>(); | ||
|
||
@BeforeAll | ||
void beforeAll() { | ||
beforeAllRandom = createRandomValueProvider(); | ||
} | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
beforeEachRandom = createRandomValueProvider(); | ||
} | ||
|
||
@AfterEach | ||
void resetTestMethodRandoms() { | ||
dataProviderRandom = null; | ||
beforeEachRandom = null; | ||
methodRandom = null; | ||
} | ||
|
||
@DataProvider | ||
Object[][] testValues1() { | ||
logger.debug("create DataProvider 1"); | ||
dataProviderRandom = createRandomValueProvider(); | ||
return $$( | ||
$(dataProviderRandom.fixedDecoratedString("1")), | ||
$(dataProviderRandom.fixedDecoratedString("2")) | ||
); | ||
} | ||
|
||
@TestTemplate | ||
@UseDataProvider("testValues1") | ||
void a_should_ensure_reproducible_ValueProvider_creation_for_DataProvider(String testValue) { | ||
assertThat(testValue).isNotEmpty(); | ||
methodRandom = createRandomValueProvider(); | ||
verifyReproducibleValueProviderCreation(dataProviderRandom, beforeEachRandom, methodRandom); | ||
} | ||
|
||
@TestTemplate | ||
@UseDataProvider("testValues1") | ||
void b_should_ensure_reproducible_ValueProvider_creation_for_same_DataProvider(String testValue) { | ||
assertThat(testValue).isNotEmpty(); | ||
methodRandom = createRandomValueProvider(); | ||
// @DataProvider is invoked ONCE BEFORE FIRST test method using it | ||
verifyReproducibleValueProviderCreation(beforeEachRandom, methodRandom); | ||
} | ||
|
||
@DataProvider | ||
Object[][] testValues2() { | ||
logger.debug("create DataProvider 2"); | ||
dataProviderRandom = createRandomValueProvider(); | ||
return $$( | ||
$(dataProviderRandom.fixedDecoratedString("1")), | ||
$(dataProviderRandom.fixedDecoratedString("2")) | ||
); | ||
} | ||
|
||
@TestTemplate | ||
@UseDataProvider("testValues2") | ||
void c_should_ensure_proper_separation_of_test_class_and_test_method_cycles_for_DataProvider(String testValue) { | ||
assertThat(testValue).isNotEmpty(); | ||
methodRandom = createRandomValueProvider(); | ||
verifyReproducibleValueProviderCreation(dataProviderRandom, beforeEachRandom, methodRandom); | ||
} | ||
|
||
@Test | ||
void d_should_ensure_reproducible_ValueProvider_creation() { | ||
methodRandom = createRandomValueProvider(); | ||
verifyReproducibleValueProviderCreation(beforeEachRandom, methodRandom); | ||
} | ||
|
||
@Test | ||
void e_should_ensure_proper_separation_of_test_class_and_test_method_cycles() { | ||
methodRandom = createRandomValueProvider(); | ||
verifyReproducibleValueProviderCreation(beforeEachRandom, methodRandom); | ||
} | ||
|
||
private void verifyReproducibleValueProviderCreation(ValueProvider... additionalMethodRandoms) { | ||
List<ValueProvider> additionalMethodRandomList = asListWithoutNulls(additionalMethodRandoms); | ||
new ValueProviderAsserter() | ||
.addExpectedTestClassRandomValues(classRandom1, classRandom2) | ||
.addExpectedTestMethodRandomValues(instanceRandom, beforeAllRandom) | ||
.addExpectedTestMethodRandomValues(randomsOfPreviousTestMethods) | ||
.addExpectedTestMethodRandomValues(additionalMethodRandomList) | ||
.assertAllTestClassRandomValues() | ||
.assertAllTestMethodRandomValues() | ||
.assertAllSuffixes(); | ||
randomsOfPreviousTestMethods.addAll(additionalMethodRandomList); | ||
} | ||
} |
Oops, something went wrong.