Skip to content

Commit

Permalink
BeforeAll and AfterAll methdos now also work.
Browse files Browse the repository at this point in the history
------------------------------------------------------------------------
On behalf of the community, the JUnit Lambda Team thanks
AdNovum Informatik AG (http://www.adnovum.ch) for supporting the
JUnit crowdfunding campaign!
------------------------------------------------------------------------
  • Loading branch information
jlink committed Dec 17, 2015
1 parent c2d9a53 commit b90f9c8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 21 deletions.
Expand Up @@ -22,9 +22,14 @@
import org.junit.gen5.api.BeforeAll;
import org.junit.gen5.api.Test;
import org.junit.gen5.api.extension.AfterAllExtensionPoint;
import org.junit.gen5.api.extension.AfterEachExtensionPoint;
import org.junit.gen5.api.extension.BeforeAllExtensionPoint;
import org.junit.gen5.api.extension.BeforeEachExtensionPoint;
import org.junit.gen5.api.extension.ContainerExtensionContext;
import org.junit.gen5.api.extension.ExtendWith;
import org.junit.gen5.api.extension.ExtensionRegistrar;
import org.junit.gen5.api.extension.ExtensionRegistry;
import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.engine.TestPlanSpecification;

/**
Expand All @@ -46,24 +51,18 @@ public void beforeAllAndAfterAllCallbacks() {

// @formatter:off
assertEquals(asList(

// "outermostBefore",
"outermostBefore",
"fooBeforeAll",
"barBeforeAll",
// "beforeAllMethod",
// "fizzBefore",
// "beforeInnerMethod",
// "innermostBefore",
"firstTest",
"secondTest",
// "innermostAfter",
// "afterInnerMethod",
// "fizzAfter",
// "afterAllMethod",
"beforeAllMethod",
"innermostBefore",
"firstTest",
"secondTest",
"innermostAfter",
"afterAllMethod",
"barAfterAll",
"fooAfterAll"
// "outermostAfter"

"fooAfterAll",
"outermostAfter"
), callSequence, "wrong call sequence");
// @formatter:on
}
Expand All @@ -72,7 +71,7 @@ public void beforeAllAndAfterAllCallbacks() {

private static List<String> callSequence = new ArrayList<>();

@ExtendWith({ FooClassLevelCallbacks.class, BarClassLevelCallbacks.class })
@ExtendWith({ FooClassLevelCallbacks.class, BarClassLevelCallbacks.class, InnermostAndOutermost.class })
private static class InstancePerMethodTestCase {

@BeforeAll
Expand All @@ -97,6 +96,33 @@ void secondTest() {
}
}

private static class InnermostAndOutermost implements ExtensionRegistrar {

@Override
public void registerExtensions(ExtensionRegistry registry) {
registry.register(this::innermostBefore, BeforeAllExtensionPoint.class, Position.INNERMOST);
registry.register(this::innermostAfter, AfterAllExtensionPoint.class, Position.INNERMOST);
registry.register(this::outermostBefore, BeforeAllExtensionPoint.class, Position.OUTERMOST);
registry.register(this::outermostAfter, AfterAllExtensionPoint.class, Position.OUTERMOST);
}

private void outermostBefore(ContainerExtensionContext context) {
callSequence.add("outermostBefore");
}

private void innermostBefore(ContainerExtensionContext context) {
callSequence.add("innermostBefore");
}

private void outermostAfter(ContainerExtensionContext context) {
callSequence.add("outermostAfter");
}

private void innermostAfter(ContainerExtensionContext context) {
callSequence.add("innermostAfter");
}
}

private static class FooClassLevelCallbacks implements BeforeAllExtensionPoint, AfterAllExtensionPoint {

@Override
Expand Down
Expand Up @@ -32,7 +32,6 @@

public class CoreJUnit5TestEngineTests extends AbstractJUnit5TestEngineTestCase {

@Ignore("https://github.com/junit-team/junit-lambda/issues/39")
@org.junit.Test
public void executeCompositeTestPlanSpecification() {
TestPlanSpecification spec = build(
Expand All @@ -48,7 +47,6 @@ public void executeCompositeTestPlanSpecification() {
Assert.assertEquals("# tests failed", 2, listener.testFailedCount.get());
}

@Ignore("https://github.com/junit-team/junit-lambda/issues/39")
@org.junit.Test
public void executeTestsForClass() {
LocalTestCase.countAfterInvoked = 0;
Expand Down
Expand Up @@ -13,19 +13,26 @@
import static org.junit.gen5.commons.util.AnnotationUtils.findAnnotatedMethods;
import static org.junit.gen5.engine.junit5.descriptor.MethodContextImpl.methodContext;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import org.junit.gen5.api.AfterAll;
import org.junit.gen5.api.AfterEach;
import org.junit.gen5.api.BeforeAll;
import org.junit.gen5.api.BeforeEach;
import org.junit.gen5.api.extension.AfterAllExtensionPoint;
import org.junit.gen5.api.extension.AfterEachExtensionPoint;
import org.junit.gen5.api.extension.BeforeAllExtensionPoint;
import org.junit.gen5.api.extension.BeforeEachExtensionPoint;
import org.junit.gen5.api.extension.ContainerExtensionContext;
import org.junit.gen5.api.extension.ExtensionConfigurationException;
import org.junit.gen5.api.extension.ExtensionPoint;
import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.commons.util.Preconditions;
Expand Down Expand Up @@ -95,6 +102,8 @@ public boolean isContainer() {
public JUnit5EngineExecutionContext beforeAll(JUnit5EngineExecutionContext context) throws Throwable {
TestExtensionRegistry newExtensionRegistry = populateNewTestExtensionRegistryFromExtendWith(testClass,
context.getTestExtensionRegistry());
registerBeforeAllMethods(newExtensionRegistry);
registerAfterAllMethods(newExtensionRegistry);
registerBeforeEachMethods(newExtensionRegistry);
registerAfterEachMethods(newExtensionRegistry);

Expand Down Expand Up @@ -156,28 +165,64 @@ private void invokeAfterAllExtensionPoints(TestExtensionRegistry newTestExtensio
TestExtensionRegistry.ApplicationOrder.BACKWARD, applyAfterAll);
}

//TODO: Remove duplication with registerAfterAllMethods
private void registerBeforeAllMethods(TestExtensionRegistry extensionRegistry) {
List<Method> beforeAllMethods = findAnnotatedMethods(testClass, BeforeAll.class, MethodSortOrder.HierarchyDown);
beforeAllMethods.stream().forEach(method -> {
if (!ReflectionUtils.isStatic(method)) {
String message = String.format(
"Cannot register method '%s' as BeforeAll extension since it is not static.", method.getName());
throw new ExtensionConfigurationException(message);
}
BeforeAllExtensionPoint extensionPoint = containerExtensionContext -> {
//TODO: Apply MethodParameterResolvers
ReflectionUtils.invokeMethod(method, null);
};
extensionRegistry.registerExtension(extensionPoint, ExtensionPoint.Position.DEFAULT, method.getName());
});
}

//TODO: Remove duplication with registerBeforeAllMethods
private void registerAfterAllMethods(TestExtensionRegistry extensionRegistry) {
List<Method> beforeAllMethods = findAnnotatedMethods(testClass, AfterAll.class, MethodSortOrder.HierarchyDown);
beforeAllMethods.stream().forEach(method -> {
if (!ReflectionUtils.isStatic(method)) {
String message = String.format(
"Cannot register method '%s' as AfterAll extension since it is not static.", method.getName());
throw new ExtensionConfigurationException(message);
}
AfterAllExtensionPoint extensionPoint = containerExtensionContext -> {
//TODO: Apply MethodParameterResolvers
ReflectionUtils.invokeMethod(method, null);
};
extensionRegistry.registerExtension(extensionPoint, ExtensionPoint.Position.DEFAULT, method.getName());
});
}

//TODO: Remove duplication with registerAfterEachMethods
private void registerBeforeEachMethods(TestExtensionRegistry extensionRegistry) {
List<Method> beforeEachMethods = findAnnotatedMethods(testClass, BeforeEach.class,
MethodSortOrder.HierarchyDown);
beforeEachMethods.stream().forEach(method -> {
BeforeEachExtensionPoint extensionPoint = testExtensionContext -> {
runMethodInExtensionContext(method, testExtensionContext, extensionRegistry);
runMethodInTestExtensionContext(method, testExtensionContext, extensionRegistry);
};
extensionRegistry.registerExtension(extensionPoint, ExtensionPoint.Position.DEFAULT, method.getName());
});
}

//TODO: Remove duplication with registerBeforeEachMethods
private void registerAfterEachMethods(TestExtensionRegistry extensionRegistry) {
List<Method> afterEachMethods = findAnnotatedMethods(testClass, AfterEach.class, MethodSortOrder.HierarchyDown);
afterEachMethods.stream().forEach(method -> {
AfterEachExtensionPoint extensionPoint = testExtensionContext -> {
runMethodInExtensionContext(method, testExtensionContext, extensionRegistry);
runMethodInTestExtensionContext(method, testExtensionContext, extensionRegistry);
};
extensionRegistry.registerExtension(extensionPoint, ExtensionPoint.Position.DEFAULT, method.getName());
});
}

private void runMethodInExtensionContext(Method method, TestExtensionContext testExtensionContext,
private void runMethodInTestExtensionContext(Method method, TestExtensionContext testExtensionContext,
TestExtensionRegistry extensionRegistry) {
Optional<Object> optionalInstance = ReflectionUtils.getOuterInstance(testExtensionContext.getTestInstance(),
method.getDeclaringClass());
Expand Down

0 comments on commit b90f9c8

Please sign in to comment.