Skip to content

Consider using assertThatExceptionOfType rather than assertThatThrownBy #2760

Closed
@philwebb

Description

@philwebb

Recently Spring Integration migrated to AssertJ and started using assertThatThrownBy() instead of an ExpectedException @Rule.

Spring Boot also made the same migration but we opted to use assertThatExceptionOfType rather than assertThatThrownBy because we felt it was a little easier to use and because it has specialized versions for IllegalArguementException, IOException and IllegalStateException.

For example, the following:

assertThatThrownBy(() -> listener.onMessage(mock(Message.class)))
				.isInstanceOf(MessageConversionException.class)
				.hasCauseInstanceOf(IllegalStateException.class);

would be:

assertThatExceptionOfType(MessageConversionException.class).isThrownBy(()->
				listener.onMessage(mock(Message.class)))
				.withCauseInstanceOf(IllegalStateException.class);

We also have a checkstyle rule to enforce the use of assertThatIllegalArgumentExceptionIsThrownBy() etc.

I thought it was worth raising an issue in case you also wanted to consider it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions