When I try to test this method
static void validatePostcode(final String postcode, final String addressLine)
{
if(! hasValidPostcode(postcode, addressLine)) {
throw new InvalidFieldException("Postcode is null or empty ");
}
}
using the following test
@Test
public void testThrowsAnException()
{
assertThatThrownBy(validatePostcode("", "")).isInstanceOf(InvalidFieldException.class);
}
I get this error message in IntelliJ
assertThatThrownBy (org.assertj.core.api.ThrowableAssert.ThrowingCallable) in Assertions cannot be applied to (void)
Same thing with assertThatExceptionOfType
.
Is it possible to test that static method actually throws an unchecked exception using AssertJ? What should I change in my test?
@Test(expected = RuntimeException.class)
but was looking to do the same using AssertJ. – Lindahl@Test(expected = ...)
. It's basically like wrapping the entire method body in a try/catch, so the test "passes" if anything in the test throws that exception, not just the thing you intend to test. So, especially for unchecked exceptions, your test can appear to pass even when it's not doing what you think it does. – MindiisInstanceOf
likehasMessage
, see assertj.github.io/doc/… – Fante