-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catch exceptions directly in the expected type #23
Comments
We are using AssertJ now, so you can easily write custom assertions for your business exception http://joel-costigliola.github.io/assertj/assertj-core-custom-assertions.html. Please show your code for catch-exception before 1.4.2. I take look into Intellij warning... |
Ok, custom assertion dosent help here. I need to think about this :) Thanks for the report |
I have only one solution to this problems -> thenCaughtException()
.isInstanceOf(IndexOutOfBoundsException.class) //
.hasMessage("Index: 1, Size: 0") //
.hasNoCause();
|
* Introducing BDDCatchException#thenCaughtException * Introducing #then(CaughtException), this is fix for #23
Fixed in #27 |
I leave old behaviour for Java 6 and 7, for Java 8 this is not working, my proposition for java 8: when(service).doSomething();
then(caughtException((BusinessRuleException.class))
.isInstanceOfAndGet(BusinessRuleException.class)
.getCode().isEqualTo(MY_ERROR_CODE); |
Is clearly better, but
still very much looks like code duplication. |
You are absolutly right, but I dont know how to do that (for Java8). http://stackoverflow.com/questions/29499847/ambiguous-method-in-java-8-why |
Can we get a non-generic version of caughtException() added? At moment the signature is
java 8 is now more strict, and requires the caller to define that type. Where method overloading means multiple parameters could match, you now get the ambiguity error. for most of the assert frameworks, we just want to pass Exception in. so it would be handier if the method signature was just:
Which would remove the ambiguity. To avoid breaking existing code, could either add non generic method with different name, or create a new parallel class that doesn't use the generics. |
Sorry for delay, I have missed your answer, I have added Btw, we can use something like |
I have a custom
BusinessRuleException
with an extra methodgetCode()
. With catch-exception 1.4.2, my tests look like this (didn't find a better way):This has 2 issues:
caughtException()
is called, IntelliJ IDEA 14.1 warns that I get an exception which is not rethrownI would expect to be able to write something like (or some variation with better API and taking into account what's required to avoid unchecked casts and whatnot):
The text was updated successfully, but these errors were encountered: