Re: Decoupling JUnit and Hamcrest

"[email protected] [junit]" <[email protected]> 11 Jan 2015 11:27:12 -0800
Newsgroups gmane.comp.java.junit.user,gmane.spam.detected
Message-ID <[email protected]>
Before there's a StefanOverflowError on this list, I have to step in... ;-)

Stefan (Bechtold), I strongly agree with most of your points. Although your fluent API looks very elegant, I think testing the properties (e.g. message, cause, and so on) of an exception is something that can and IMHO should also be done using the assertion framework that was chosen by the user. 

We are currently discussing a very slim approach to state that a block of code is expected to throw an exception of a certain class. If it does not throw an exception of the expected class you'll get an AssertionError. Now, to check the exception message etc. you can use the assertion framework of your choice, e.g. Hamcrest:

public void test() {
    // setup

    IllegalArgumentException thrown = expect(IllegalArgumentException.class, () -> doSomethingThatThrows());

    //optionally assert something about the exception
    assertThat(thrown, hasMessage(startsWith("foo")));
}

For details/feedback please see the full discussion at:
https://github.com/junit-team/junit/pull/1055

Cheers,

Marc


------------------------------------
Posted by: [email protected]
------------------------------------