Skip to content

Commit

Permalink
Run all afterEach even if one fails
Browse files Browse the repository at this point in the history
  • Loading branch information
greghaskins committed Feb 20, 2016
1 parent d2e58a4 commit 7d14b66
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/main/java/com/greghaskins/spectrum/AfterEachBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.greghaskins.spectrum;

import java.util.ArrayList;
import java.util.List;

import com.greghaskins.spectrum.Spectrum.Block;

class AfterEachBlock implements Block {

private final List<Block> blocks;

public AfterEachBlock() {
this.blocks = new ArrayList<Block>();
}

@Override
public void run() throws Throwable {
runAllBlocksInReverseOrder(this.blocks.size() - 1);
}

private void runAllBlocksInReverseOrder(final int index) throws Throwable {
if (index < 0) {
return;
}
try {
this.blocks.get(index).run();
} finally {
runAllBlocksInReverseOrder(index - 1);
}
}

public void addBlock(final Block block) {
this.blocks.add(block);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/greghaskins/spectrum/Suite.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Suite implements Parent, Child {
private final CompositeBlock afterAll = new CompositeBlock();

private final CompositeBlock beforeEach = new CompositeBlock();
private final CompositeBlock afterEach = new CompositeBlock();
private final AfterEachBlock afterEach = new AfterEachBlock();

private final List<Child> children = new ArrayList<Child>();
private final Set<Child> focusedChildren = new HashSet<Child>();
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/specs/FixturesSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,40 @@ public class FixturesSpec {{

});

describe("when another afterEach explodes", () -> {

it("still run, too", () -> {
final Result result = SpectrumRunner.run(getSuiteWithExplodingAndNonExplodingAfterEach());
assertThat(result.getFailureCount(), is(1));
assertThat(result.getFailures().get(0).getMessage(), containsString("boom"));
});

});

final ArrayList<String> items = new ArrayList<>();

describe("in multiples", () -> {

it("run in reverse order", () -> {
assertThat(items, hasSize(0));
});

afterEach(() -> {
items.add("after1");
});
afterEach(() -> {
items.add("after2");
});
afterEach(() -> {
items.add("after3");
});

});

it("run in reverse declaration order", () -> {
assertThat(items, contains("after3", "after2", "after1"));
});

});

}
Expand Down Expand Up @@ -425,4 +459,35 @@ class Suite {{
return Suite.class;
}

private static Class<?> getSuiteWithExplodingAndNonExplodingAfterEach(){

class Suite {{
describe("suite with exploding spec", () -> {

final ArrayList<String> items = new ArrayList<>();

describe("boom", () -> {
it("explodes", () -> {
items.add("foo");
});

afterEach(() -> {
throw new Exception("boom");
});

afterEach(()->{
items.clear();
});
});

it("should still run afterEach blocks", () -> {
assertThat(items, hasSize(0));
});


});
}}
return Suite.class;
}

}

0 comments on commit 7d14b66

Please sign in to comment.