Skip to content

Commit 49f1c5d

Browse files
committed
Polish ThreadsTest
1 parent faa0e33 commit 49f1c5d

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

src/test/java/org/junit/tests/running/classes/ThreadsTest.java

+20-28
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33
import org.junit.AfterClass;
44
import org.junit.Test;
5-
import org.junit.internal.runners.ErrorReportingRunner;
65
import org.junit.runner.Description;
76
import org.junit.runner.JUnitCore;
87
import org.junit.runner.Request;
98
import org.junit.runner.Result;
109
import org.junit.runner.RunWith;
11-
import org.junit.runner.Runner;
1210
import org.junit.runner.notification.RunListener;
1311
import org.junit.runners.BlockJUnit4ClassRunner;
14-
import org.junit.runners.model.InitializationError;
1512

13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
import static java.util.Arrays.asList;
17+
import static java.util.Collections.singletonList;
1618
import static org.junit.Assert.assertEquals;
1719

1820
public class ThreadsTest {
19-
private String log = "";
21+
22+
private List<Boolean> interruptedFlags = new ArrayList<Boolean>();
23+
private JUnitCore core = new JUnitCore();
2024

2125
public static class TestWithInterrupt {
2226

@@ -34,18 +38,17 @@ public void otherTestCaseInterruptingCurrentThread() {
3438

3539
@Test
3640
public void currentThreadInterruptedStatusIsClearedAfterEachTestExecution() {
37-
log = "";
38-
JUnitCore jUnitCore = new JUnitCore();
39-
jUnitCore.addListener(new RunListener() {
41+
core.addListener(new RunListener() {
4042
@Override
4143
public void testFinished(Description description) {
42-
log += Thread.currentThread().isInterrupted() + " ";
44+
interruptedFlags.add(Thread.currentThread().isInterrupted());
4345
}
4446
});
4547

46-
Result result = jUnitCore.run(TestWithInterrupt.class);
48+
Result result = core.run(TestWithInterrupt.class);
49+
4750
assertEquals(0, result.getFailureCount());
48-
assertEquals("false false ", log);
51+
assertEquals(asList(false, false), interruptedFlags);
4952
}
5053

5154
@RunWith(BlockJUnit4ClassRunner.class)
@@ -63,29 +66,18 @@ public void test() {
6366

6467
@Test
6568
public void currentThreadInterruptStatusIsClearedAfterSuiteExecution() {
66-
log = "";
67-
JUnitCore jUnitCore = new JUnitCore();
68-
jUnitCore.addListener(new RunListener() {
69+
core.addListener(new RunListener() {
6970
@Override
70-
public void testSuiteFinished(Description description) throws Exception {
71-
log += Thread.currentThread().isInterrupted();
71+
public void testSuiteFinished(Description description) {
72+
interruptedFlags.add(Thread.currentThread().isInterrupted());
7273
}
7374
});
7475

75-
Request request = new Request() {
76-
@Override
77-
public Runner getRunner() {
78-
try {
79-
return new BlockJUnit4ClassRunner(TestWithInterruptFromAfterClass.class) {
80-
};
81-
} catch (InitializationError e) {
82-
return new ErrorReportingRunner(TestWithInterruptFromAfterClass.class, e);
83-
}
84-
}
85-
};
76+
Request request = Request.aClass(TestWithInterruptFromAfterClass.class);
77+
78+
Result result = core.run(request);
8679

87-
Result result = jUnitCore.run(request);
8880
assertEquals(0, result.getFailureCount());
89-
assertEquals("false", log);
81+
assertEquals(singletonList(false), interruptedFlags);
9082
}
9183
}

0 commit comments

Comments
 (0)