我只想测试一下是否使用google-truth抛出了给定消息的异常。
使用@Test(expected=使用junit很容易做到这一点,但是我不知道如何使用事实来做到这一点。ThrowableSubject周围没有任何示例。
对于这类测试,我应该坚持使用普通的JUnit吗?
发布于 2016-08-10 15:04:21
已更新
Truth的作者建议使用JUnit 4.13/5的assertThrows()机制,因为这并不真正需要Truth的支持。这看起来更像是:
SpecificException e =
assertThrows(SpecificException.class, () -> doSomethingThatThrows());
assertThat(e).hasMessageThat().contains("blah blah blah");
assertThat(e).hasCauseThat().isInstanceOf(IllegalStateException.class);
assertThat(e).hasCauseThat().hasMessageThat().contains("blah");推荐使用这种方法而不是尝试/失败/捕获,因为它更简洁,避免了“丢失失败”的问题,并返回一个可以使用ThrowableSubject in Truth断言的对象。
如果您没有assertThrows(),那么请使用try/fail/catch模式,因为这是明确和明确的。
try {
doSomethingThatThrows();
fail("method should throw");
} catch (SpecificException e) {
// ensure that e was thrown from the right code-path
// especially important if it's something as frequent
// as an IllegalArgumentException, etc.
assertThat(e).hasMessage("blah blah blah");
}虽然@Rule ExpectedException和@Test(exception=...)存在于JUnit中,但Truth团队并不推荐它们,因为它们有一些微妙的(和不那么微妙的)方法,您可以编写通过但应该失败的测试。
虽然尝试/失败/捕获也是如此,但谷歌内部通过使用error-prone缓解了这一问题,它提供了静态编译时检查,以确保此模式不会省略fail()等。强烈建议您使用容易出错的或其他静态分析检查来捕获这些错误。遗憾的是,基于规则和基于注释的方法不像try/catch块那样容易接受静态分析。
发布于 2017-06-06 17:00:29
作为一个更新,我们已经远离了克里斯蒂安描述的模式,Issue #219已经被关闭,取而代之的是JUnit的expectThrows() (在4.13中,类似的方法在TestNG's Assert中已经存在)。
结合expectThrows(),你可以使用Truth来制作assertions about the thrown exception。所以克里斯蒂安的例子现在是:
SpecificException expected = expectThrows(
SpecificException.class, () -> doSomethingThatThrows());
assertThat(expected).hasMessageThat().contains("blah blah blah");发布于 2016-07-20 02:42:08
目前还没有内置的方法来使用google-truth验证预期的Exception。您可以执行以下操作之一:
正如@c0der所提到的,which is what the unit tests for guava do
一样,在进行“丑陋”的测试时使用one of the JUnit approaches,比如expected=,用try...catch包围测试的/动作/when部分
我相信google-truth没有任何类似的功能,因为它的supports Java 1.6。
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.Subject;
import com.google.common.truth.SubjectFactory;
import org.junit.Test;
import java.util.concurrent.Callable;
import static com.google.common.truth.Truth.assertAbout;
public class MathTest {
@Test
public void addExact_throws_ArithmeticException_upon_overflow() {
assertAbout(callable("addExact"))
.that(() -> Math.addExact(Integer.MAX_VALUE, 1))
.willThrow(ArithmeticException.class);
}
static <T> SubjectFactory<CallableSubject<T>, Callable<T>> callable(String displaySubject) {
return new SubjectFactory<CallableSubject<T>, Callable<T>>() {
@Override public CallableSubject<T> getSubject(FailureStrategy fs, Callable<T> that) {
return new CallableSubject<>(fs, that, displaySubject);
}
};
}
static class CallableSubject<T> extends Subject<CallableSubject<T>, Callable<T>> {
private final String displaySubject;
CallableSubject(FailureStrategy failureStrategy, Callable<T> callable, String displaySubject) {
super(failureStrategy, callable);
this.displaySubject = displaySubject;
}
@Override protected String getDisplaySubject() {
return displaySubject;
}
void willThrow(Class<?> clazz) {
try {
getSubject().call();
fail("throws a", clazz.getName());
} catch (Exception e) {
if (!clazz.isInstance(e)) {
failWithBadResults("throws a", clazz.getName(), "throws a", e.getClass().getName());
}
}
}
}
}https://stackoverflow.com/questions/38434769
复制相似问题