Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertJ: How to return the actual exception when using method assertThatThrownBy

I am migrating from JUnit 5 and Hamcrest Assertions to AssertJ and I can't figure out the right method for extracting the actual exception from the executable. Here is a JUnit/Hamcrest example:

var result = assertThrows(MyException.class, () -> this.objectUnderTest.someMethod(param1, param2, param3));
assertThat(result.getError().getErrorCode(), equalTo(someValue));
assertThat(result.getError().getDescription(), equalTo("someString"));

What I would like to have is smth. like (AssertJ)

var result = assertThatThrownBy(() -> this.objectUnderTest.someMethod(param1, param2, param3))
 .isInstanceOf(MyException.class);
assertThat(result.getError().getErrorCode(), equalTo(someValue));
assertThat(result.getError().getDescription(), equalTo("someString"));

But as of the AssertJ version 3.21.0 the assertThatThrownBy method gives me an instance of AbstractThrowableAssert<?, ? extends Throwable> class and I can't find any method which would give me then the MyException instance. So for now I ended up using another method and casting manually to MyException:

Throwable throwable = Assertions.catchThrowable(() -> this.objectUnderTest.doFilterInternal(param1, param2, param3));
MyException exception = (MyException) throwable;
assertThat(exception.getError().getErrorCode(), equalTo(someValue));
assertThat(exception.getError().getDescription(), equalTo("someString"));
like image 285
Arthur Eirich Avatar asked Oct 27 '25 12:10

Arthur Eirich


2 Answers

AssertJ offers two styles for checking custom exceptions:

  • BDD style with catchThrowableOfType
  • Single statement style with assertThatExceptionOfType

Keeping the style of your example, you can write the following with catchThrowable:

Throwable throwable = catchThrowable(() -> this.objectUnderTest.someMethod(param1, param2, param3));

assertThat(throwable)
  .asInstanceOf(InstanceOfAssertFactories.throwable(MyException.class))
  .extracting(MyException::getError)
  .returns(1, from(Error::getErrorCode))
  .returns("something", from(Error::getDescription));

or, with catchThrowableOfType:

MyException exception = catchThrowableOfType(() -> this.objectUnderTest.someMethod(param1, param2, param3), MyException.class);

assertThat(exception)
  .extracting(MyException::getError)
  .returns(1, from(Error::getErrorCode))
  .returns("something", from(Error::getDescription));
like image 152
Stefano Cordio Avatar answered Oct 29 '25 01:10

Stefano Cordio


var result = assertThatThrownBy(() ->
this.objectUnderTest.someMethod(param1, param2, param3)) 
.isInstanceOf(MyException.class);
assertThat(result.getError().getErrorCode(), equalTo(someValue));
assertThat(result.getError().getDescription(), equalTo("someString"));

The AssertJ way is following:

assertThatExceptionOfType(MyException.class)
        .isThrownBy(() -> this.objectUnderTest.someMethod(param1, param2, param3))
        .satisfies(ex -> assertThat(ex.getError().getErrorCode()).isEqualTo(someValue));
like image 43
Andrey B. Panfilov Avatar answered Oct 29 '25 00:10

Andrey B. Panfilov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!