-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
This matcher is requested from practical needs, when trying to solve the following test scenario. It is currently implemented out of JUnit, and working fine against tests.
The test case is testing code statements, where their call sequences matter a lot and some of them throw exception.
Which exception is expected, and where in the call sequence, all depends on the test code itself.
Thus suppose this formal test case:
@test(expected = SomeException.class)
public void myTest() {
statement
statement
This block must not throw SomeException
{
statement
statement
statement
}
statement
statement
}
In this particular scenarion the developer dos not want to encapsulate the middle part into an ugly and dangerous try-catch block.
Here the test hehaviour wants to say that all statements are supposed to throw SomeException, except the middle part. This means that we want to keep Hamcrest in use with assertThat() and keep the tests as much verbose as possible.
The IsThrowable Matcher has four variations, where the last two are able to put the stack trace from the failing block to description (output) stream:
throwing(Class<? extends Throwable>... ): Matcher<IsThrowable.IBlock>
notThrowing(Class<? extends Throwable>... ): Matcher<IsThrowable.IBlock>
throwingDeeply(Class<? extends Throwable>... ): Matcher<IsThrowable.IBlock>
notThrowingDeeply(Class<? extends Throwable>... ): Matcher<IsThrowable.IBlock>
The form of is(not(throwing(...))) is equivalent to the form is(notThrowing(...)).
Similar with the form of is(not(throwingDeeply(...))) which is again equivalent to is(notThrowingDeeply(...)).
Once you use the 'Deeply', you can see the stack trace of failed block.
The allOf/anyOf, and/or combination forms of multiple Matchers would not be much readable, however still correct.