forked from greghaskins/spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleSpec.java
94 lines (65 loc) · 2.29 KB
/
ExampleSpec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package specs;
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.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.runner.RunWith;
import com.greghaskins.spectrum.Spectrum;
import com.greghaskins.spectrum.Spectrum.Block;
@RunWith(Spectrum.class)
public class ExampleSpec {{
describe("A spec", () -> {
final int foo = 1;
it("is just a code block with a run() method", new Block() {
@Override
public void run() throws Throwable {
assertEquals(1, foo);
}
});
it("can also be a lambda function, which is a lot prettier", () -> {
assertEquals(1, foo);
});
it("can use any assertion library you like", () -> {
org.junit.Assert.assertEquals(1, foo);
org.hamcrest.MatcherAssert.assertThat(true, is(true));
});
describe("nested inside a second describe", () -> {
final int bar = 1;
it("can reference both scopes as needed", () -> {
assertThat(bar, is(equalTo(foo)));
});
});
});
describe("A spec using beforeEach", () -> {
final List<String> items = new ArrayList<String>();
beforeEach(() -> {
items.clear();
});
beforeEach(() -> {
items.add("foo");
});
beforeEach(() -> {
items.add("bar");
});
it("runs the beforeEach() blocks in order", () -> {
assertThat(items, contains("foo", "bar"));
});
it("runs them before every test", () -> {
assertThat(items, contains("foo", "bar"));
});
describe("when nested", () -> {
beforeEach(() -> {
items.add("baz");
});
it("runs beforeEach() blocks from inner and outer scopes", () -> {
assertThat(items, contains("foo", "bar", "baz"));
});
});
});
}}