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
Provide native support for expected exceptions #121
Comments
Warning: using @rule in the Spectrum test clas with ExpectedException has some gotchas. It will bleed state between tests in a way that doesn't happen in JUnit, which creates a new test class instance each time. The JUnitMixin approach will resolve that, but maybe we should provide a Spectrum native mechanism and document the whole subject? |
It turns out that the I've made a POC for a Spectrum native approach to this. Undoubtedly it has some rough edges, but a commit illustrating it is here - ashleyfrieze@6896018 - please offer feedback in the comment thread within this issue. Hopefully when #119 is merged, I can produce a PR for this, which is presently branched off #119's codebase as it uses some of it. |
I think expected exceptions fall under the realm of an assertion library. Reasons:
The good news, however, is that I'm also not a huge fan of the AssertJ way to assert exception scenarios. It's just verbose and not very clear. My preference would be something like: expect(() -> obj.doSomething(foo))
.to(raise(SomeException.class)); And I'm experimenting with creating just that, over in https://github.com/spectrumbdd/spectrum-expectations. This issue has caused me to re-consider whether to there should be a Spectrum-provided set of assertions. An alternative, runner-only solution might just be some syntactic sugar around Optional<Throwable> thrownException = catchException(() -> obj.doSomething(foo));
assertThat(thrownException).isPresent();
assertThat(thrownException.get().getMessage()).isEqualTo("hello"); |
Did you have a look at what I implemented? It was quite nice in the end. Your suggestion for an exception assertion looked similar to assertJ to me. |
Closing this issue per our discussion on Gitter. For those browsing through this issue in the future (hi there! 👋 ), here's the context:
|
There are a few ways to test for some code ending in an exception.
AssertJ
withassertThatThrownBy
E.g.
ExpectedException
rule from JUnit either withjunitMixin
or even raw with@Rule
E.g.
aroundEach
Each of these is plausible (though the last one sucks a bit), but not quite as neat as
@Test(expected=SomeException.class) public void myTest() {}
that you get in JUnit. Here are two motivations for some alternative:context
ordescribe
a bunch of things that end in the same exceptionThere are a couple of ways I could imagine doing this and I'd like feedback on which might be desirable:
BlockConfiguration
as part of thewith
syntaxlet
to provide the expected exception similar toExpectedException
but more native toSpectrum
Block Configuraton Approach
This is hierarchical and can be applied anywhere in the tree
e.g.
Hook approach
Let's use
ExpectedException
within Spectrum, or a clone of it if we don't want it to be too JUnit. What we end up with will be similar tolet
but will add an expected exception hook instead of alet hook
.e.g.
@greghaskins - your thoughts pls.
The text was updated successfully, but these errors were encountered: