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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package org.junit.tests.manipulation;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.List;
import junit.framework.JUnit4TestAdapter;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runners.Parameterized;
import org.junit.runners.Suite;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Suite.SuiteClasses;
public class SingleMethodTest {
public static int count;
static public class OneTimeSetup {
@BeforeClass public static void once() {
count++;
}
@Test public void one() {
}
@Test public void two() {
}
}
@Test public void oneTimeSetup() throws Exception {
count = 0;
Runner runner = Request.method(OneTimeSetup.class, "one").getRunner();
Result result = new JUnitCore().run(runner);
assertEquals(1, count);
assertEquals(1, result.getRunCount());
}
@RunWith(Parameterized.class)
static public class ParameterizedOneTimeSetup {
@Parameters
public static List<Object[]> params() {
return Arrays.asList(new Object[] {1}, new Object[] {2});
}
public ParameterizedOneTimeSetup(int x) {
}
@Test public void one() {
}
}
@Test public void parameterizedFilterToSingleMethod() throws Exception {
count = 0;
Runner runner = Request.method(ParameterizedOneTimeSetup.class,
"one[0]").getRunner();
Result result = new JUnitCore().run(runner);
assertEquals(1, result.getRunCount());
}
@RunWith(Parameterized.class)
static public class ParameterizedOneTimeBeforeClass {
@Parameters
public static List<Object[]> params() {
return Arrays.asList(new Object[] {1}, new Object[] {2});
}
public ParameterizedOneTimeBeforeClass(int x) {
}
@BeforeClass public static void once() {
count++;
}
@Test public void one() {
}
}
@Test public void parameterizedBeforeClass() throws Exception {
count = 0;
JUnitCore.runClasses(ParameterizedOneTimeBeforeClass.class);
assertEquals(1, count);
}
@Test public void filteringAffectsPlan() throws Exception {
Runner runner = Request.method(OneTimeSetup.class, "one").getRunner();
assertEquals(1, runner.testCount());
}
@Test public void nonexistentMethodCreatesFailure() throws Exception {
assertEquals(1, new JUnitCore().run(
Request.method(OneTimeSetup.class, "thisMethodDontExist"))
.getFailureCount());
}
@Test(expected = NoTestsRemainException.class)
public void filteringAwayEverythingThrowsException() throws NoTestsRemainException {
Filterable runner = (Filterable) Request.aClass(OneTimeSetup.class).getRunner();
runner.filter(new Filter() {
@Override
public boolean shouldRun(Description description) {
return false;
}
@Override
public String describe() {
return null;
}
});
}
public static class TestOne {
@Test public void a() {
}
@Test public void b() {
}
}
public static class TestTwo {
@Test public void a() {
}
@Test public void b() {
}
}
@RunWith(Suite.class)
@SuiteClasses( { TestOne.class, TestTwo.class })
public static class OneTwoSuite {
}
@Test public void eliminateUnnecessaryTreeBranches() throws Exception {
Runner runner = Request.aClass(OneTwoSuite.class).filterWith(
Description.createTestDescription(TestOne.class, "a"))
.getRunner();
Description description = runner.getDescription();
assertEquals(1, description.getChildren().size());
}
public static class HasSuiteMethod {
@Test public void a() {}
@Test public void b() {}
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(HasSuiteMethod.class);
}
}
@Test public void classesWithSuiteMethodsAreFiltered() {
int testCount= Request.method(HasSuiteMethod.class, "a").getRunner().getDescription().testCount();
assertThat(testCount, is(1));
}
}
|