Skip to content

Instantly share code, notes, and snippets.

@relax-more
Created November 9, 2015 12:04
Show Gist options
  • Save relax-more/d440a6672a45585d8db1 to your computer and use it in GitHub Desktop.
Save relax-more/d440a6672a45585d8db1 to your computer and use it in GitHub Desktop.
how to assert Exception with TypeSafeMatcher
public class AreTest {
@Test
public void test_WithTypeSafeMatcher() throws Exception {
when(service.getAre()).thenThrow(ApiException.class);
thrown.expect(new TypeSafeMatcher<ApiException>() {
@Override
public void describeTo(Description description) {
description.appendText("expects EXPECTED_ERROR_CODE");
}
@Override
protected boolean matchesSafely(ApiException item) {
return item.getResultCode() == ResultCode.EXPECTED_ERROR_CODE;
}
});
target.get();
}
public class Are {
@Inject
AreService service;
public void get () {
service.getAre();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment