Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More tests; fix case if failed and finished throw exceptions

  • Loading branch information...
commit 2162def2d4eefae0c5668586b837af4ecd4aed91 1 parent 8c1c0e7
David Hart davidhart82 authored
60 src/main/java/org/junit/rules/TestWatcher.java
View
@@ -48,29 +48,60 @@ public Statement apply(final Statement base, final Description description) {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
+
+ startingQuietly(description, errors);
try {
- starting(description);
- try {
- base.evaluate();
- succeeded(description);
- } catch (AssumptionViolatedException e) {
- throw e;
- } catch (Throwable t) {
- errors.add(t);
- failed(t, description);
- } finally {
- finished(description);
- }
+ base.evaluate();
+ succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
throw e;
} catch (Throwable t) {
errors.add(t);
+ failedQuietly(t, description, errors);
+ } finally {
+ finishedQuietly(description, errors);
}
- if (!errors.isEmpty())
- throw new MultipleFailureException(errors);
+
+ MultipleFailureException.assertEmpty(errors);
}
};
}
+
+ private void succeededQuietly(Description description,
+ List<Throwable> errors) {
+ try {
+ succeeded(description);
+ } catch (Throwable t) {
+ errors.add(t);
+ }
+ }
+
+ private void failedQuietly(Throwable t, Description description,
+ List<Throwable> errors) {
+ try {
+ failed(t, description);
+ } catch (Throwable t1) {
+ errors.add(t1);
+ }
+ }
+
+ private void startingQuietly(Description description,
+ List<Throwable> errors) {
+ try {
+ starting(description);
+ } catch (Throwable t) {
+ errors.add(t);
+ }
+ }
+
+ private void finishedQuietly(Description description,
+ List<Throwable> errors) {
+ try {
+ finished(description);
+ } catch (Throwable t) {
+ errors.add(t);
+ }
+ }
/**
* Invoked when a test succeeds
@@ -97,7 +128,6 @@ protected void failed(Throwable e, Description description) {
protected void starting(Description description) {
}
-
/**
* Invoked when a test method finishes (whether passing or failing)
*
59 src/test/java/org/junit/tests/experimental/rules/TestWatcherTest.java
View
@@ -55,7 +55,7 @@ public void logFailingTest() {
is("starting failed finished "));
}
- public static class TestWatcherThrowsExceptionTest {
+ public static class TestWatcherFailedThrowsExceptionTest {
@Rule
public TestRule watcher= new TestWatcher() {
@Override
@@ -71,11 +71,62 @@ public void fails() {
}
@Test
- public void testWatcherThrowsException() {
- PrintableResult result= testResult(TestWatcherThrowsExceptionTest.class);
+ public void testWatcherFailedThrowsException() {
+ PrintableResult result= testResult(TestWatcherFailedThrowsExceptionTest.class);
assertThat(result, failureCountIs(2));
assertThat(result, hasFailureContaining("test failure"));
assertThat(result, hasFailureContaining("watcher failure"));
-
}
+
+ public static class TestWatcherStartingThrowsExceptionTest {
+ @Rule
+ public TestRule watcher= new TestWatcher() {
+ @Override
+ protected void starting(Description description) {
+ throw new RuntimeException("watcher failure");
+ }
+ };
+
+ @Test
+ public void fails() {
+ throw new IllegalArgumentException("test failure");
+ }
+ }
+
+ @Test
+ public void testWatcherStartingThrowsException() {
+ PrintableResult result= testResult(TestWatcherStartingThrowsExceptionTest.class);
+ assertThat(result, failureCountIs(2));
+ assertThat(result, hasFailureContaining("test failure"));
+ assertThat(result, hasFailureContaining("watcher failure"));
+ }
+
+ public static class TestWatcherFailedAndFinishedThrowsExceptionTest {
+ @Rule
+ public TestRule watcher= new TestWatcher() {
+ @Override
+ protected void failed(Throwable t, Description description) {
+ throw new RuntimeException("watcher failed failure");
+ }
+
+ @Override
+ protected void finished(Description description) {
+ throw new RuntimeException("watcher finished failure");
+ }
+ };
+
+ @Test
+ public void fails() {
+ throw new IllegalArgumentException("test failure");
+ }
+ }
+
+ @Test
+ public void testWatcherFailedAndFinishedThrowsException() {
+ PrintableResult result= testResult(TestWatcherFailedAndFinishedThrowsExceptionTest.class);
+ assertThat(result, failureCountIs(3));
+ assertThat(result, hasFailureContaining("test failure"));
+ assertThat(result, hasFailureContaining("watcher failed failure"));
+ assertThat(result, hasFailureContaining("watcher finished failure"));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.