Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run onceBefore/onceAfter methods also for spec superclasses #12

Merged
merged 1 commit into from
Jan 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions jdave-core/src/java/jdave/runner/SpecRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ public <T extends Specification<?>> void run(Class<T> specType, ISpecVisitor cal
runOnceAfters(specType);
}

private <T extends Specification<?>> void runOnceBefores(Class<T> specType) {
runPublicStaticVoidMethodNamed("onceBefore", specType);
private <T extends Specification<?>> void runOnceBefores(Class<?> specType) {
if (specType != null) {
runOnceBefores(specType.getSuperclass());
runPublicStaticVoidMethodNamed("onceBefore", specType);
}
}

private <T extends Specification<?>> void runOnceAfters(Class<T> specType) {
runPublicStaticVoidMethodNamed("onceAfter", specType);
private <T extends Specification<?>> void runOnceAfters(Class<?> specType) {
if (specType != null) {
runPublicStaticVoidMethodNamed("onceAfter", specType);
runOnceAfters(specType.getSuperclass());
}
}

private <T> void runPublicStaticVoidMethodNamed(String name, Class<T> specType) {
Expand Down Expand Up @@ -113,4 +119,4 @@ private boolean annotationIsPresent(Class<?> clazz, Class<? extends Annotation>
}
return false;
}
}
}
26 changes: 25 additions & 1 deletion jdave-core/src/test/jdave/runner/SpecRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ public void testShouldCallOnceAfterExactlyOnce() throws Exception {
assertEquals(1, BooleanSpec.onceAfterCalled);
}

@Test
public void testShouldCallSuperClassOnceBeforeExactlyOnce() throws Exception {
BaseSpec.onceBeforeCalled = 0;
runner.run(BooleanSpec.class, new SpecVisitorAdapter(new ResultsAdapter()));
assertEquals(1, BaseSpec.onceBeforeCalled);
}

@Test
public void testShouldCallSuperClassOnceAfterExactlyOnce() throws Exception {
BaseSpec.onceAfterCalled = 0;
runner.run(BooleanSpec.class, new SpecVisitorAdapter(new ResultsAdapter()));
assertEquals(1, BaseSpec.onceAfterCalled);
}

@Test
public void testShouldCallOnceAfterEvenIfContextDestroyCrashes() {
SpecWithCrashingContextDestroy.onceAfterCalled = 0;
Expand Down Expand Up @@ -174,6 +188,16 @@ public void testAllowsContextWithoutCreateMethod() throws Exception {

public static class BaseSpec extends Specification<Boolean> {
public static List<String> actualCalls = new ArrayList<String>();
public static int onceBeforeCalled;
public static int onceAfterCalled;

public static void onceBefore() {
onceBeforeCalled++;
}

public static void onceAfter() {
onceAfterCalled++;
}

public class CommonContext {
public void anyBehavior() {
Expand Down Expand Up @@ -368,4 +392,4 @@ public void someBehavior() {
}
}
}
}
}