-
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.
Merge pull request #43 from TNG/41-add-support-for-junit-5-lifecycle-…
…per-class Add support for JUnit 5 @nested test classes and Lifecycle PER_CLASS
- Loading branch information
Showing
25 changed files
with
1,241 additions
and
290 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
32 changes: 23 additions & 9 deletions
32
core/src/main/java/com/tngtech/valueprovider/ValueProviderException.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 |
---|---|---|
@@ -1,29 +1,43 @@ | ||
package com.tngtech.valueprovider; | ||
|
||
import static com.tngtech.valueprovider.InitializationCreator.VALUE_PROVIDER_FACTORY_REFERENCE_DATE_TIME_PROPERTY; | ||
import static com.tngtech.valueprovider.InitializationCreator.VALUE_PROVIDER_FACTORY_TEST_CLASS_SEED_PROPERTY; | ||
import static com.tngtech.valueprovider.InitializationCreator.VALUE_PROVIDER_FACTORY_TEST_METHOD_SEED_PROPERTY; | ||
import static com.tngtech.valueprovider.ValueProviderFactory.getFormattedReferenceDateTime; | ||
import static com.tngtech.valueprovider.ValueProviderFactory.getTestClassSeed; | ||
import static com.tngtech.valueprovider.ValueProviderFactory.getTestMethodSeed; | ||
import java.util.Optional; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import static com.tngtech.valueprovider.InitializationCreator.*; | ||
import static com.tngtech.valueprovider.ValueProviderFactory.*; | ||
import static java.lang.String.format; | ||
import static java.util.Optional.empty; | ||
|
||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") | ||
public class ValueProviderException extends RuntimeException { | ||
ValueProviderException() { | ||
super(provideFailureReproductionInfo()); | ||
this(empty()); | ||
} | ||
|
||
ValueProviderException(Optional<Class<?>> testClassToReRunForReproduction) { | ||
super(provideFailureReproductionInfo(testClassToReRunForReproduction)); | ||
} | ||
|
||
static String provideFailureReproductionInfo() { | ||
@VisibleForTesting | ||
static String provideFailureReproductionInfo(Optional<Class<?>> testClassToReRunForReproduction) { | ||
long testClassSeed = getTestClassSeed(); | ||
long testMethodSeed = getTestMethodSeed(); | ||
String referenceDateTime = getFormattedReferenceDateTime(); | ||
return format( | ||
"If the failure is related to random ValueProviders, specify the following system properties for the JVM to reproduce:%n" + | ||
"If the failure is related to random ValueProviders, %sspecify the following system properties for the JVM to reproduce:%n" + | ||
"-D%s=%d%n" + | ||
"-D%s=%d%n" + | ||
"-D%s=%s", | ||
formatReRunMessageFor(testClassToReRunForReproduction), | ||
VALUE_PROVIDER_FACTORY_TEST_CLASS_SEED_PROPERTY, testClassSeed, | ||
VALUE_PROVIDER_FACTORY_TEST_METHOD_SEED_PROPERTY, testMethodSeed, | ||
VALUE_PROVIDER_FACTORY_REFERENCE_DATE_TIME_PROPERTY, referenceDateTime); | ||
} | ||
|
||
private static String formatReRunMessageFor(Optional<Class<?>> testClassToReRunForReproduction) { | ||
return testClassToReRunForReproduction.map(testClass -> | ||
format("re-run all tests of '%s' and ", testClass.getName())) | ||
.orElse(""); | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
doc/junit5-lifecycle-PER_CLASS-reproducible-test-class-and-method-cycle.puml
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,111 @@ | ||
@startuml | ||
participant "JVM\nJUnit\nRuntime" as JVM | ||
participant "Test A,\nLifecycle\n'PER_CLASS'" as T_A | ||
|
||
participant "VPExtension\n(JUnit5)" as VPF_X #lightblue | ||
participant "ValueProviderFactory" as VPF | ||
note over VPF: Just shown for single thread,\nimplemented as ThreadLocal\nfor parallel test execution | ||
participant "DefaultInitializationCreator" as D_VPC | ||
note over D_VPC: 1. Uses arbitrary/random seed\n2. Does NOT ensure unique suffixes | ||
participant "TestClassInitializationCreator" as C_VPC | ||
participant "TestMethodInitializationCreator" as M_VPC | ||
note over C_VPC, M_VPC: 1. Seed and reference date/time\n controllable via system properties\n2. Ensures unique suffixes\n (as far as possible) | ||
|
||
JVM --> VPF : class-loading & instantiation | ||
activate VPF | ||
VPF -> D_VPC : instantiation | ||
activate D_VPC | ||
VPF -> M_VPC : instantiation | ||
activate M_VPC | ||
VPF -> VPF : activateCreator(DefaultInitializationCreator) | ||
|
||
== Test A class-loading == | ||
JVM --> VPF_X : instantiation | ||
activate VPF_X #lightblue | ||
JVM -> VPF_X : beforeAll | ||
note right #lightgreen: nothing to do for Lifecycle PER_CLASS | ||
JVM -> VPF_X : interceptTestClassConstructor | ||
VPF_X -> VPF : startTestClassCycle | ||
VPF -> C_VPC : instantiation | ||
activate C_VPC | ||
note right of C_VPC #lightgreen: different lifecycle required\nfor tests with JUnit4 DataProvider | ||
VPF -> C_VPC : startTestCycle | ||
C_VPC -> C_VPC : initialize seed &\n reference date/time | ||
note right of C_VPC #lightgreen: using system properties | ||
VPF -> VPF : activateCreator(TestClassInitializationCreator) | ||
VPF_X -> JVM : trigger class-loading of Test A | ||
|
||
T_A -> VPF : createRandomValueProvider | ||
note left #lightgreen: static initialization | ||
VPF -> C_VPC : createRandomValueProvider | ||
|
||
VPF_X -> VPF : startTestMethodCycle | ||
VPF -> VPF : activateCreator(TestMethodInitializationCreator) | ||
VPF -> M_VPC : startTestCycle(TestClassInitializationCreator) | ||
M_VPC -> M_VPC : initialize seed &\n reference date/time | ||
note right of M_VPC #lightgreen: using system properties | ||
M_VPC -> M_VPC : copyValueProviderSuffixes(TestClassInitializationCreator) | ||
note right of M_VPC #lightgreen: required to ensure unique suffixes | ||
M_VPC -> M_VPC : copyReferenceDateTime(TestClassInitializationCreator) | ||
note right of M_VPC #lightgreen: required to ensure reproducible\ndate/time related test data | ||
JVM <-- VPF_X : proceed | ||
== Test A test method <1> == | ||
JVM --> T_A : instantiation (once for all test methods) | ||
activate T_A | ||
|
||
T_A -> VPF : createRandomValueProvider | ||
note left #lightgreen: instance variables | ||
VPF -> M_VPC : createRandomValueProvider | ||
|
||
T_A -> VPF : createRandomValueProvider | ||
note left #lightgreen: before methods | ||
VPF -> M_VPC : createRandomValueProvider | ||
|
||
JVM -> T_A : test method <1> | ||
T_A -> VPF : createRandomValueProvider | ||
note left #lightgreen: test method | ||
VPF -> M_VPC : createRandomValueProvider | ||
|
||
T_A --> JVM : return | ||
JVM -> VPF_X : afterEach | ||
note right #lightgreen: nothing to do for Lifecycle PER_CLASS | ||
newpage | ||
|
||
== Test A test method <2> == | ||
T_A -> VPF : createRandomValueProvider | ||
note left #lightgreen | ||
(note: instance variables shared) | ||
before methods | ||
end note | ||
VPF -> M_VPC : createRandomValueProvider | ||
|
||
JVM -> T_A : test method <2> | ||
T_A -> VPF : createRandomValueProvider | ||
note left #lightgreen: test method | ||
VPF -> M_VPC : createRandomValueProvider | ||
|
||
T_A --> JVM : return | ||
JVM -> VPF_X : afterEach | ||
note right #lightgreen: nothing to do for Lifecycle PER_CLASS | ||
|
||
JVM --> T_A : destruction | ||
destroy T_A | ||
|
||
JVM -> VPF_X : afterAll | ||
|
||
VPF_X -> VPF : finishTestMethodCycle | ||
VPF -> M_VPC : finishTestCycle | ||
M_VPC -> M_VPC : resetValueProviderSuffixes | ||
VPF -> VPF : activateCreator(TestClassInitializationCreator) | ||
|
||
VPF_X -> VPF : finishTestClassCycle | ||
VPF -> C_VPC : finishTestCycle | ||
C_VPC -> C_VPC : resetValueProviderSuffixes | ||
VPF -> VPF : delete TestClassInitializationCreator | ||
destroy C_VPC | ||
VPF -> VPF : activateCreator(DefaultInitializationCreator) | ||
|
||
JVM --> VPF_X | ||
destroy VPF_X | ||
|
||
@enduml |
Oops, something went wrong.