summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/experimental/rules/TestWatchmanTest.java
blob: 9ef22cb93d6310df9aca47c8942117345f67935b (plain)
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
package org.junit.tests.experimental.rules;

import static junit.framework.Assert.fail;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.junit.runner.JUnitCore.runClasses;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatchman;
import org.junit.runners.model.FrameworkMethod;

@SuppressWarnings("deprecation")
public class TestWatchmanTest {
	public static class ViolatedAssumptionTest {
		@Rule
		public static LoggingTestWatchman watchman= new LoggingTestWatchman();

		@Test
		public void succeeds() {
			assumeTrue(false);
		}
	}

	@Test
	public void neitherLogSuccessNorFailedForViolatedAssumption() {
		runClasses(ViolatedAssumptionTest.class);
		assertThat(ViolatedAssumptionTest.watchman.log.toString(),
				is("starting finished "));
	}

	public static class FailingTest {
		@Rule
		public static LoggingTestWatchman watchman= new LoggingTestWatchman();

		@Test
		public void succeeds() {
			fail();
		}
	}

	@Test
	public void logFailingTest() {
		runClasses(FailingTest.class);
		assertThat(FailingTest.watchman.log.toString(),
				is("starting failed finished "));
	}

	private static class LoggingTestWatchman extends TestWatchman {
		private final StringBuilder log= new StringBuilder();

		@Override
		public void succeeded(FrameworkMethod method) {
			log.append("succeeded ");
		}

		@Override
		public void failed(Throwable e, FrameworkMethod method) {
			log.append("failed ");
		}

		@Override
		public void starting(FrameworkMethod method) {
			log.append("starting ");
		}

		@Override
		public void finished(FrameworkMethod method) {
			log.append("finished ");
		}
	}
}