-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
ExpectedException#expect() should take Matcher<? extends Throwable> as its argument #610
Comments
In my experience, getting stricter with Matcher type parameters almost always results in unexpected negative consequences, similar to the much more verbose required syntax you mention. What is the error message thrown in the above case? |
The error message is java.lang.AssertionError:
Expected: (an instance of java.lang.RuntimeException and a string starting with "some message")
but: a string starting with "some message" was a java.lang.RuntimeException (<java.lang.RuntimeException: some message>)
... which is correct and gives you a hint what the problem might be. But when you don't read the error message carefully you might wonder why the test isn't green. |
I'll consider it, but I'm not sure it will be worth the type pain. |
@UrsMetz Note that we are considering removing our Hamcrest dependencies, so you might want to file this bug against the java-hamcrest project (https://github.com/hamcrest/hamcrest-junit). |
@kcooney I re-raised this issue at hamcrest-junit: hamcrest/hamcrest-junit#12. |
@UrsMetz Thanks! |
Consider the following test:
When you have something like this in a test it can be quite hard to figure out why the test fails.
At the moment
ExpectedException#expect
takes aMatcher<?>
as its argument. By changing it to take only arguments of the typeMatcher<? extends Throwable>
the compiler could spot the mistake in the test given above.I tried the code changes needed for the suggested change in the API with the current master (92a3f73) and all tests pass.
I understand that these changes break the API and non-generic
Matcher
s won't work with the new signature of the method but is there any other reason not to haveExpectedException#expect
only take arguments of the typeMatcher<? extends Throwable>
?Apart from the exclusion of non-generic
Matcher
s the only downside I see is that now you have to specify the generic type: That is you have to write for exampleinstead of
The text was updated successfully, but these errors were encountered: