GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — improve isThrown() method error msg by simhani1 · Pull Request #3847 · assertj/assertj

Conversation

@simhani1

@simhani1 simhani1 commented Jun 4, 2025

Copy link
Copy Markdown
Contributor

Description

This PR improves the clarity of error messages when no exception is thrown by a lambda passed to isThrownBy.

Previously, the failure message simply stated that “code should raise a throwable,” which could be vague in some contexts.
Now, the message explicitly mentions the expected exception type, e.g.:

Expecting code to throw java.lang.IllegalArgumentException, but no exception was thrown.

This change enhances the developer experience by providing more precise feedback during assertion failures, especially when working with soft assertions or parameterized exception checks.

I also updated related tests to align with the new message format.

Check List:

  • Fixes Mention expected Throwable type in failure message for ThrowableTypeAssert.isThrownBy() #3845
  • Unit tests : YES / NO / NA
  • Javadoc with a code example (on API only) : YES / NO / NA

Following the contributing guidelines will make it easier for us to review and accept your PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

centrumek reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@simhani1

simhani1 commented Jun 4, 2025

Copy link
Copy Markdown
Contributor Author

While working on this change, I encountered a design concern regarding the following logic:

public ThrowableAssertAlternative<T> isThrownBy(final ThrowingCallable throwingCallable) {
  Throwable throwable = Throwables.catchThrowable(throwingCallable);
  checkThrowableType(throwable);
  @SuppressWarnings("unchecked")
  T castThrowable = (T) throwable;
  return buildThrowableTypeAssert(castThrowable).as(description);
}

protected void checkThrowableType(Throwable throwable) {
  assertThat(throwable).as(description).hasBeenThrown().isInstanceOf(expectedThrowableType);
}

Originally, hasBeenThrown() is used to check whether an exception was thrown, and it’s implemented in AbstractThrowableAssert.

However, hasBeenThrown() only checks whether actual is null, which means it cannot include the expected exception type in the failure message — because actual itself is the thrown exception instance that would need to be checked.

protected SELF hasBeenThrown() {
  if (actual == null) {
    throw Failures.instance().failure(info, new BasicErrorMessageFactory("%nExpecting code to raise a throwable."));
  }
  return myself;
}

So I make a new method hasThrownExceptionOf(Class<? extends Throwable>), which accepts the expected exception type and produces a more informative failure message when no exception is thrown, such as:

Expecting code to throw java.lang.IllegalArgumentException, but no exception was thrown.

I’m open to suggestions if there’s a better way to include the expected exception type in the failure message.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@scordio scordio added this to the 4.0.0-M2 milestone Jun 4, 2025
@simhani1 simhani1 changed the title improve is thrown by error msg improve isThrown() method error msg Jun 4, 2025
@simhani1
simhani1 marked this pull request as draft June 5, 2025 05:21
simhani1 added 6 commits June 6, 2025 17:55
@simhani1
feat: add assertion to verify thrown exception type
cc854c1
@simhani1
refactor: use hasThrownExceptionOf for clearer exception type assertion
b0790fa
@simhani1
style: add missing period to failure message in hasThrownExceptionOf
5873fe6
@simhani1
test: update test to match new failure message format
e507c3e
@simhani1
style: apply Spotless formatting
b3c072c
@simhani1
test: update test to match new failure message format
40cdab6
@simhani1
simhani1 force-pushed the simhani1/3845-improve-isThrownBy-error-msg branch from 63688e7 to 40cdab6 Compare June 6, 2025 08:56
@simhani1
simhani1 marked this pull request as ready for review June 6, 2025 08:56
@joel-costigliola
joel-costigliola self-requested a review August 30, 2025 22:21
@joel-costigliola

joel-costigliola commented Sep 1, 2025

Copy link
Copy Markdown
Member

Integrated thanks @simhani1 !

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@joel-costigliola joel-costigliola closed this Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@joel-costigliola joel-costigliola Awaiting requested review from joel-costigliola

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.0.0-M2

Development

Successfully merging this pull request may close these issues.

Mention expected Throwable type in failure message for ThrowableTypeAssert.isThrownBy()

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@simhani1 @joel-costigliola @scordio
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.