junit5 - How do I use Hamcrest with JUnit 5 when JUnit 5 doesn't have an assertThat() function? -
to use hamcrest junit 4 use assertthat()
function. however, junit 5 no longer going have assertthat()
function. how use hamcrest without assertthat()
?
you have make sure hamcrest included in classpath , use assertthat() function provided hamcrest. current junit 5 user guide - writing tests assertions,
junit jupiter’s org.junit.jupiter.assertions class not provide assertthat() method 1 found in junit 4’s org.junit.assert class accepts hamcrest matcher. instead, developers encouraged use built-in support matchers provided third-party assertion libraries.
the following example demonstrates how use assertthat() support hamcrest in junit jupiter test. long hamcrest library has been added classpath, can statically import methods such assertthat(), is(), , equalto() , use them in tests in assertwithhamcrestmatcher() method below.
import static org.hamcrest.corematchers.equalto; import static org.hamcrest.corematchers.is; import static org.hamcrest.matcherassert.assertthat; import org.junit.jupiter.api.test; class hamcrestassertiondemo { @test void assertwithhamcrestmatcher() { assertthat(2 + 1, is(equalto(3))); } }
naturally, legacy tests based on junit 4 programming model can continue using org.junit.assert#assertthat."
Comments
Post a Comment