在 Android Espresso 测试中断言异常



我在Espresso中有一个测试,需要断言某个操作会导致引发异常。

然而,似乎Espresso框架吞噬了原始异常,只出现了一个PerformException

你可以这样做

@Test( expected = ArrayIndexOutOfBoundsException.class) //OR any other  
throwable
public void myTest() {
}

最终我找到了一种方法。 我创建了一个自定义 Hamcrest 匹配器,允许您验证嵌套异常。

public class NestedExceptionMatcher extends TypeSafeMatcher<Throwable> {
private final Class<?> mExpectedType;
private final Matcher<String> mMessageMatcher;
static NestedExceptionMatcher expectNestedThrowable(Class<?> expectedType, Matcher<String> messageMatcher) {
return new NestedExceptionMatcher(expectedType, messageMatcher);
}
NestedExceptionMatcher(Class<?> expectedType, Matcher<String> messageMatcher) {
mExpectedType = expectedType;
mMessageMatcher = messageMatcher;
}
@Override
protected boolean matchesSafely(Throwable item) {
boolean matches = isMatch(item);
Throwable currentThrowable = item.getCause();
while (!matches && currentThrowable != null) {
matches = isMatch(currentThrowable);
currentThrowable = currentThrowable.getCause();
}
return matches;
}
@Override
public void describeTo(Description description) {
description
.appendText("expects type ")
.appendValue(mExpectedType)
.appendText(" with a message ")
.appendDescriptionOf(mMessageMatcher);
}
private boolean isMatch(Throwable t) {
return t.getClass().isAssignableFrom(mExpectedType) && mMessageMatcher.matches(t.getMessage());
}
}

然后,您可以在测试中按如下方式使用它:

public class SomeActivityTest {
@Rule
public ActivityTestRule<SomeActivity> mRule = new ActivityTestRule<>(
SomeActivity.class, false, false);
@Rule
public ExpectedException mExceptionRule = ExpectedException.none();
@Test
public void testClick_whenInvalidParamsAreSet_shouldThrowException() {
mRule.launchActivity(getIntentWithInvalidParams());
mExceptionRule.expect(expectNestedThrowable(
IllegalArgumentException.class,
containsString("parameter isn't defined")
));
onView(withId(R.id.my_btn)).perform(click());
}
private Intent getIntentWithInvalidParams() {
...
}
}

为什么不使用它:

@Test(expected = PerformException::class)
fun traderSummaryContainer_notScroll() {
onView(withId(R.id.traderSummaryContainer))
.perform(scrollTo())
}

使用正常的尝试捕获解决了我的问题,我知道异常是嵌套的。 但我们仍然可以按原因检查它:

var error: Throwable? = null
try {
onView(something).perform(click())
} catch (e: Throwable) {
error = e;
}
assert(error!!.cause is MySpecificException)
assert(error!!.cause!!.message == "My specific error message")

我已经尝试了上述自定义Hamcrest匹配器方法,但我最终进行了片状测试,因此最终进入了这种方法。

为什么不使用 Assert.assertThrows(Class expectThrowable, ThrowingRunnable runnable( 或 Assert.assertThrows(字符串消息、类预期可抛出、投掷运行可运行

(

最新更新