Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

More tests; fix case if failed and finished throw exceptions

  • Loading branch information...
commit 2162def2d4eefae0c5668586b837af4ecd4aed91 1 parent 8c1c0e7
David Hart authored June 24, 2012
60  src/main/java/org/junit/rules/TestWatcher.java
@@ -48,29 +48,60 @@ public Statement apply(final Statement base, final Description description) {
48 48
 			@Override
49 49
 			public void evaluate() throws Throwable {
50 50
 				List<Throwable> errors = new ArrayList<Throwable>();
  51
+
  52
+				startingQuietly(description, errors);
51 53
 				try {
52  
-					starting(description);
53  
-					try {
54  
-						base.evaluate();
55  
-						succeeded(description);
56  
-					} catch (AssumptionViolatedException e) {
57  
-						throw e;
58  
-					} catch (Throwable t) {
59  
-						errors.add(t);
60  
-						failed(t, description);
61  
-					} finally {
62  
-						finished(description);
63  
-					}
  54
+					base.evaluate();
  55
+					succeededQuietly(description, errors);
64 56
 				} catch (AssumptionViolatedException e) {
65 57
 					throw e;
66 58
 				} catch (Throwable t) {
67 59
 					errors.add(t);
  60
+					failedQuietly(t, description, errors);
  61
+				} finally {
  62
+					finishedQuietly(description, errors);
68 63
 				}
69  
-				if (!errors.isEmpty())
70  
-					throw new MultipleFailureException(errors);
  64
+				
  65
+				MultipleFailureException.assertEmpty(errors);
71 66
 			}
72 67
 		};
73 68
 	}
  69
+
  70
+	private void succeededQuietly(Description description,
  71
+			List<Throwable> errors) {
  72
+		try {
  73
+			succeeded(description);
  74
+		} catch (Throwable t) {
  75
+			errors.add(t);
  76
+		}
  77
+	}
  78
+	
  79
+	private void failedQuietly(Throwable t, Description description,
  80
+			List<Throwable> errors) {
  81
+		try {
  82
+			failed(t, description);
  83
+		} catch (Throwable t1) {
  84
+			errors.add(t1);
  85
+		}
  86
+	}
  87
+
  88
+	private void startingQuietly(Description description, 
  89
+			List<Throwable> errors) {
  90
+		try {
  91
+			starting(description);
  92
+		} catch (Throwable t) {
  93
+			errors.add(t);
  94
+		}
  95
+	}
  96
+	
  97
+	private void finishedQuietly(Description description,
  98
+			List<Throwable> errors) {
  99
+		try {
  100
+			finished(description);
  101
+		} catch (Throwable t) {
  102
+			errors.add(t);
  103
+		}
  104
+	}
74 105
 	
75 106
 	/**
76 107
 	 * Invoked when a test succeeds
@@ -97,7 +128,6 @@ protected void failed(Throwable e, Description description) {
97 128
 	protected void starting(Description description) {
98 129
 	}
99 130
 
100  
-
101 131
 	/**
102 132
 	 * Invoked when a test method finishes (whether passing or failing)
103 133
 	 * 
59  src/test/java/org/junit/tests/experimental/rules/TestWatcherTest.java
@@ -55,7 +55,7 @@ public void logFailingTest() {
55 55
 				is("starting failed finished "));
56 56
 	}
57 57
 	
58  
-	public static class TestWatcherThrowsExceptionTest {
  58
+	public static class TestWatcherFailedThrowsExceptionTest {
59 59
 		@Rule
60 60
 		public TestRule watcher= new TestWatcher() {
61 61
 			@Override
@@ -71,11 +71,62 @@ public void fails() {
71 71
 	}
72 72
 	
73 73
 	@Test
74  
-	public void testWatcherThrowsException() {
75  
-		PrintableResult result= testResult(TestWatcherThrowsExceptionTest.class);
  74
+	public void testWatcherFailedThrowsException() {
  75
+		PrintableResult result= testResult(TestWatcherFailedThrowsExceptionTest.class);
76 76
 		assertThat(result, failureCountIs(2));
77 77
 		assertThat(result, hasFailureContaining("test failure"));
78 78
 		assertThat(result, hasFailureContaining("watcher failure"));
79  
-		
80 79
 	}
  80
+	
  81
+	public static class TestWatcherStartingThrowsExceptionTest {
  82
+		@Rule
  83
+		public TestRule watcher= new TestWatcher() {
  84
+			@Override
  85
+			protected void starting(Description description) {
  86
+				throw new RuntimeException("watcher failure");
  87
+			}
  88
+		};
  89
+
  90
+		@Test
  91
+		public void fails() {
  92
+			throw new IllegalArgumentException("test failure");
  93
+		}
  94
+	}
  95
+	
  96
+	@Test
  97
+	public void testWatcherStartingThrowsException() {
  98
+		PrintableResult result= testResult(TestWatcherStartingThrowsExceptionTest.class);
  99
+		assertThat(result, failureCountIs(2));
  100
+		assertThat(result, hasFailureContaining("test failure"));
  101
+		assertThat(result, hasFailureContaining("watcher failure"));
  102
+	}
  103
+	
  104
+	public static class TestWatcherFailedAndFinishedThrowsExceptionTest {
  105
+		@Rule
  106
+		public TestRule watcher= new TestWatcher() {
  107
+			@Override
  108
+			protected void failed(Throwable t, Description description) {
  109
+				throw new RuntimeException("watcher failed failure");
  110
+			}
  111
+			
  112
+			@Override
  113
+			protected void finished(Description description) {
  114
+				throw new RuntimeException("watcher finished failure");
  115
+			}
  116
+		};
  117
+
  118
+		@Test
  119
+		public void fails() {
  120
+			throw new IllegalArgumentException("test failure");
  121
+		}
  122
+	}
  123
+	
  124
+	@Test
  125
+	public void testWatcherFailedAndFinishedThrowsException() {
  126
+		PrintableResult result= testResult(TestWatcherFailedAndFinishedThrowsExceptionTest.class);
  127
+		assertThat(result, failureCountIs(3));
  128
+		assertThat(result, hasFailureContaining("test failure"));
  129
+		assertThat(result, hasFailureContaining("watcher failed failure"));
  130
+		assertThat(result, hasFailureContaining("watcher finished failure"));
  131
+	}	
81 132
 }

0 notes on commit 2162def

Please sign in to comment.
Something went wrong with that request. Please try again.