Avoid catching AssertionError in tests

Found via ErrorProne
This commit is contained in:
Eric Anderson 2017-09-18 09:54:14 -07:00
parent 6bb1755357
commit bd28b92850
2 changed files with 22 additions and 16 deletions

View File

@ -455,7 +455,7 @@ public class ContextTest {
assertSame(current, observed);
assertSame(current, Context.current());
final Error err = new Error();
final TestError err = new TestError();
try {
base.wrap(new Runnable() {
@Override
@ -464,7 +464,7 @@ public class ContextTest {
}
}).run();
fail("Expected exception");
} catch (Error ex) {
} catch (TestError ex) {
assertSame(err, ex);
}
assertSame(current, Context.current());
@ -495,7 +495,7 @@ public class ContextTest {
assertSame(current, observed);
assertSame(current, Context.current());
final Error err = new Error();
final TestError err = new TestError();
try {
base.wrap(new Callable<Object>() {
@Override
@ -504,7 +504,7 @@ public class ContextTest {
}
}).call();
fail("Excepted exception");
} catch (Error ex) {
} catch (TestError ex) {
assertSame(err, ex);
}
assertSame(current, Context.current());
@ -987,4 +987,7 @@ public class ContextTest {
}
}
}
/** Allows more precise catch blocks than plain Error to avoid catching AssertionError. */
private static final class TestError extends Error {}
}

View File

@ -1027,7 +1027,7 @@ public class ServerImplTest {
ServerStreamListener mockListener = mock(ServerStreamListener.class);
listener.setListener(mockListener);
Throwable expectedT = new AssertionError();
TestError expectedT = new TestError();
doThrow(expectedT).when(mockListener)
.messagesAvailable(any(StreamListener.MessageProducer.class));
// Closing the InputStream is done by the delegated listener (generally ServerCallImpl)
@ -1035,7 +1035,7 @@ public class ServerImplTest {
try {
executor.runDueTasks();
fail("Expected exception");
} catch (Throwable t) {
} catch (TestError t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
}
@ -1052,7 +1052,7 @@ public class ServerImplTest {
ServerStreamListener mockListener = mock(ServerStreamListener.class);
listener.setListener(mockListener);
Throwable expectedT = new RuntimeException();
RuntimeException expectedT = new RuntimeException();
doThrow(expectedT).when(mockListener)
.messagesAvailable(any(StreamListener.MessageProducer.class));
// Closing the InputStream is done by the delegated listener (generally ServerCallImpl)
@ -1060,7 +1060,7 @@ public class ServerImplTest {
try {
executor.runDueTasks();
fail("Expected exception");
} catch (Throwable t) {
} catch (RuntimeException t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
}
@ -1077,13 +1077,13 @@ public class ServerImplTest {
ServerStreamListener mockListener = mock(ServerStreamListener.class);
listener.setListener(mockListener);
Throwable expectedT = new AssertionError();
TestError expectedT = new TestError();
doThrow(expectedT).when(mockListener).halfClosed();
listener.halfClosed();
try {
executor.runDueTasks();
fail("Expected exception");
} catch (Throwable t) {
} catch (TestError t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
}
@ -1100,13 +1100,13 @@ public class ServerImplTest {
ServerStreamListener mockListener = mock(ServerStreamListener.class);
listener.setListener(mockListener);
Throwable expectedT = new RuntimeException();
RuntimeException expectedT = new RuntimeException();
doThrow(expectedT).when(mockListener).halfClosed();
listener.halfClosed();
try {
executor.runDueTasks();
fail("Expected exception");
} catch (Throwable t) {
} catch (RuntimeException t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
}
@ -1123,13 +1123,13 @@ public class ServerImplTest {
ServerStreamListener mockListener = mock(ServerStreamListener.class);
listener.setListener(mockListener);
Throwable expectedT = new AssertionError();
TestError expectedT = new TestError();
doThrow(expectedT).when(mockListener).onReady();
listener.onReady();
try {
executor.runDueTasks();
fail("Expected exception");
} catch (Throwable t) {
} catch (TestError t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
}
@ -1146,13 +1146,13 @@ public class ServerImplTest {
ServerStreamListener mockListener = mock(ServerStreamListener.class);
listener.setListener(mockListener);
Throwable expectedT = new RuntimeException();
RuntimeException expectedT = new RuntimeException();
doThrow(expectedT).when(mockListener).onReady();
listener.onReady();
try {
executor.runDueTasks();
fail("Expected exception");
} catch (Throwable t) {
} catch (RuntimeException t) {
assertSame(expectedT, t);
ensureServerStateNotLeaked();
}
@ -1249,4 +1249,7 @@ public class ServerImplTest {
throw new UnsupportedOperationException();
}
}
/** Allows more precise catch blocks than plain Error to avoid catching AssertionError. */
private static final class TestError extends Error {}
}