Skip to content

Commit

Permalink
add example of beforeAll and afterAll
Browse files Browse the repository at this point in the history
along with cautionary note
  • Loading branch information
greghaskins committed Dec 2, 2014
1 parent b7072a5 commit 0fc3ed2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,41 @@ public class ExampleSpec {{

});

describe("A spec using beforeAll", () -> {

final List<Integer> numbers = new ArrayList<Integer>();

beforeAll(() -> {
numbers.add(1);
});

it("sets the initial state before any tests run", () -> {
assertThat(numbers, contains(1));
numbers.add(2);
});

describe("and afterAll", () -> {

afterAll(() -> {
numbers.clear();
});

it("does not reset anything between tests", () -> {
assertThat(numbers, contains(1, 2));
numbers.add(3);
});

it("so proceed with caution; this *will* leak shared state across tests", () -> {
assertThat(numbers, contains(1, 2, 3));
});
});

it("cleans up after running all tests in the describe block", () -> {
assertThat(numbers, is(empty()));
});

});

}}
```

Expand Down
38 changes: 38 additions & 0 deletions src/test/java/specs/ExampleSpec.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package specs;

import static com.greghaskins.spectrum.Spectrum.afterAll;
import static com.greghaskins.spectrum.Spectrum.afterEach;
import static com.greghaskins.spectrum.Spectrum.beforeAll;
import static com.greghaskins.spectrum.Spectrum.beforeEach;
import static com.greghaskins.spectrum.Spectrum.describe;
import static com.greghaskins.spectrum.Spectrum.it;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -103,4 +106,39 @@ public void run() throws Throwable {

});

describe("A spec using beforeAll", () -> {

final List<Integer> numbers = new ArrayList<Integer>();

beforeAll(() -> {
numbers.add(1);
});

it("sets the initial state before any tests run", () -> {
assertThat(numbers, contains(1));
numbers.add(2);
});

describe("and afterAll", () -> {

afterAll(() -> {
numbers.clear();
});

it("does not reset anything between tests", () -> {
assertThat(numbers, contains(1, 2));
numbers.add(3);
});

it("so proceed with caution; this *will* leak shared state across tests", () -> {
assertThat(numbers, contains(1, 2, 3));
});
});

it("cleans up after running all tests in the describe block", () -> {
assertThat(numbers, is(empty()));
});

});

}}

0 comments on commit 0fc3ed2

Please sign in to comment.