Skip to content

Instantly share code, notes, and snippets.

@sebersole
Created November 2, 2017 17:07
Show Gist options
  • Save sebersole/98843a920e40d44aa23bc0166b16b626 to your computer and use it in GitHub Desktop.
Save sebersole/98843a920e40d44aa23bc0166b16b626 to your computer and use it in GitHub Desktop.
@TestFactory
public final List<DynamicNode> generateNodes() throws Exception {
final Class<? extends EnversBaseTest> testClass = getClass();
final List<DynamicNode> nodes = new ArrayList<>();
final List<Method> testMethods = findMethods( testClass, method -> method.isAnnotationPresent( EnversTest.class ) );
final List<Method> beforeEachMethods = findMethods( testClass, method -> method.isAnnotationPresent( EnversBeforeEach.class ) );
final List<Method> afterEachMethods = findMethods( testClass, method -> method.isAnnotationPresent( EnversAfterEach.class ) );
for ( String strategy : strategies ) {
final EnversBaseTest testClassInstanceForStrategy = testClass.newInstance();
final EnversSessionFactoryScope scope = new EnversSessionFactoryScope( testClassInstanceForStrategy, strategy );
testClassInstanceForStrategy.sessionFactoryScope = scope;
final List<DynamicTest> tests = new ArrayList<>();
for ( Method method : testMethods ) {
tests.add(
DynamicTest.dynamicTest(
method.getName(),
() -> {
// todo : need some form of @BeforeAll / @AfterAll callbacks
// - how to hook these in though? before-all is easy enough
// but what exactly is the condition that tells
// us we have executed all tests?
for ( Method beforeEachMethod : beforeEachMethods ) {
beforeEachMethod.invoke( testClassInstanceForStrategy );
}
Exception invocationException = null;
try {
method.invoke( testClassInstanceForStrategy );
}
catch (Exception e) {
invocationException = e;
throw e;
}
finally {
try {
for ( Method afterEachMethod : afterEachMethods ) {
afterEachMethod.invoke( testClassInstanceForStrategy );
}
}
catch (Exception e) {
if ( invocationException == null ) {
throw e;
}
}
}
}
)
);
}
nodes.add(
DynamicContainer.dynamicContainer(
testClass.getName() + " (" + strategy + ')',
tests
)
);
}
return nodes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment