The testing guidelines at Google have the following to say about testing for exceptions: "The strongly preferred solution is to use Assert.assertThrows(). ... In some situations you will decide it is important to check additional details about the thrown exception. You might care about its message, or the attributes of its chained exception, for example. It is easy to check these details, as assertThrows returns the thrown exception to you, and you can pass it to Truth. ... There are two cases where you can't use assertThrows: * In open-sourced code, until JUnit 4.13 is released. * When you aren't able to use Java 8 constructs like lambda expressions. (You can still use assertThrows, but you may find it bulky enough to outweigh its advantages.) However, use caution when doing so ... Don't use ExpectedException, @Test(expected =), or ExceptionExpectations. You can find a lengthy explanation of the pitfalls of these approaches in this page's history." I've mentioned these pitfalls in code reviews before, based on this advice. Paraphrasing the page history referenced above: * The main downside of manual try-fail is it's easy to forget the fail call; we've been bitten by this in Gerrit several times. * ExpectedException is dangerous because any statements after the throwing call will never be executed, even though the author of those statements probably did not intend this. * @Test(expected) passes if *any* statement in the test body throws, regardless of which statement the author had in mind. Later statements may not execute at all. Unfortunately, JUnit 4.13 is taking much longer to release than anticipated. I asked the Truth team approximately one year ago whether it would make sense to add assertThrows to Truth itself; their answer was that they didn't think it was worth it, given that it would eventually be replaced by JUnit 4.13. They told me that if we want to use it sooner, we should roll our own, so here we are. I wrote this method to be source-compatible with the method in JUnit 4.13 based upon reading the method signature and Javadoc of JUnit. However, I wrote the implementation from scratch without referencing the JUnit implementation. Change-Id: I4a774846ad6245f418c50be02dc469698b6e4def
68 lines
2.3 KiB
Java
68 lines
2.3 KiB
Java
// Copyright (C) 2019 The Android Open Source Project
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package com.google.gerrit.testing;
|
|
|
|
/** Static JUnit utility methods. */
|
|
public class GerritJUnit {
|
|
/**
|
|
* Assert that an exception is thrown by a block of code.
|
|
*
|
|
* <p>This method is source-compatible with <a
|
|
* href="https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThrows(java.lang.Class,%20org.junit.function.ThrowingRunnable)">JUnit
|
|
* 4.13 beta</a>.
|
|
*
|
|
* <p>This construction is recommended by the Truth team for use in conjunction with asserting
|
|
* over a {@code ThrowableSubject} on the return type:
|
|
*
|
|
* <pre>
|
|
* MyException e = assertThrows(MyException.class, () -> doSomething(foo));
|
|
* assertThat(e).isInstanceOf(MySubException.class);
|
|
* assertThat(e).hasMessageThat().contains("sub-exception occurred");
|
|
* </pre>
|
|
*
|
|
* @param throwableClass expected exception type.
|
|
* @param runnable runnable containing arbitrary code.
|
|
* @return exception that was thrown.
|
|
*/
|
|
public static <T extends Throwable> T assertThrows(
|
|
Class<T> throwableClass, ThrowingRunnable runnable) {
|
|
try {
|
|
runnable.run();
|
|
} catch (Throwable t) {
|
|
if (!throwableClass.isInstance(t)) {
|
|
throw new AssertionError(
|
|
"expected "
|
|
+ throwableClass.getName()
|
|
+ " but "
|
|
+ t.getClass().getName()
|
|
+ " was thrown",
|
|
t);
|
|
}
|
|
@SuppressWarnings("unchecked")
|
|
T toReturn = (T) t;
|
|
return toReturn;
|
|
}
|
|
throw new AssertionError(
|
|
"expected " + throwableClass.getName() + " but no exception was thrown");
|
|
}
|
|
|
|
@FunctionalInterface
|
|
public interface ThrowingRunnable {
|
|
void run() throws Throwable;
|
|
}
|
|
|
|
private GerritJUnit() {}
|
|
}
|