Skip to content

Add assertThatCode(Callable) - #2519

Draft
scordio wants to merge 4 commits into
3.xfrom
callable-assert
Draft

scordio wants to merge 4 commits into
3.xfrom
callable-assert

Conversation

@scordio

@scordio scordio commented Mar 8, 2022 •

Copy link
Copy Markdown
Member

Usage examples in CallableAssert_Demo_Test.

This introduces a breaking change for all the assertThatCode() assertions with a Callable that don't call doesNotThrowAnyException() but any other method from AbstractThrowableAssert.

Check List:

@scordio
scordio requested a review from joel-costigliola March 8, 2022 00:38
@joel-costigliola

Copy link
Copy Markdown
Member

LGTM :)

@joel-costigliola

Copy link
Copy Markdown
Member

I think we can drop my attempt https://github.com/assertj/assertj-core/pull/2516/files

@scordio scordio mentioned this pull request Mar 9, 2022
Comment thread src/main/java/org/assertj/core/api/AbstractCallableAssert.java Outdated

@joel-costigliola joel-costigliola left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented

@joel-costigliola

joel-costigliola commented Mar 12, 2022 •

Copy link
Copy Markdown
Member

Slightly off topic, I think the existing assertThatCode is not great as it exposes throwable assertions, even if the code throws one, it does not read very nicely:

// why would we expect to have an exception here?
assertThatCode(() -> doStuff()).hasMessage("Boom!");

your version with callable is much better in that sense.
The existing alternative is:

// reads nicely
assertThatThrownBy(() -> doStuff()).hasMessage("Boom!");

Ideally I would replace the existing assertThatCode(ThrowingCallable) by your version but that's probably something we should do for AssertJ 4 as it's a might be too big of a breaking change (not sure how to evaluate this). What we could do though is deprecate assertThatCode(ThrowingCallable) in favor of assertThatThrownBy or assertThatCode(Callable).

@scordio

scordio commented Mar 13, 2022

Copy link
Copy Markdown
Member Author

Slightly off topic, I think the existing assertThatCode is not great as it exposes throwable assertions, even if the code throws one, it does not read very nicely

Yes, I tried to say that but with wrong wording 😆

I'm exploring the direction of ThrowingAssert for assertThatCode(Callable) and maybe it's not so pricey to introduce ThrowingRunnable and the corresponding assertThatCode as an alternative for assertThatCode(ThrowingCallable).

@scordio
scordio force-pushed the callable-assert branch 2 times, most recently from a9145d3 to 976bf14 Compare March 13, 2022 16:21
@scordio

scordio commented Mar 13, 2022 •

Copy link
Copy Markdown
Member Author

@joel-costigliola I went with ThrowingExecutableAssert, maybe clearer than just ThrowingAssert and harder to be confused with ThrowableAssert. Could you have a look and let me know what you think?

I'd experiment the replacement of assertThatCode(ThrowingCallable) in a separate PR/branch once this one is stable. I have the feeling it would be a binary incompatible change but source compatible at the same time, as long as no one used weird patterns like assertThatCode(...).hasMessage(...).

@scordio

scordio commented Mar 17, 2022 •

Copy link
Copy Markdown
Member Author

With the new assertion hierarchy (ThrowingExecutableAssert / AbstractThrowingExecutableAssert), a new assumeThatCode(Callable) cannot be easily added as all the code around Byte Buddy supports only AbstractAssert types.

We might add it in a second round, keeping only assumeThatCode(ThrowingRunnable) for the time being.

@scordio
scordio force-pushed the callable-assert branch 2 times, most recently from 2e46f25 to e1f4a9a Compare April 9, 2022 20:40
}

@Override
protected void execute(Callable<V> actual) throws Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth specifying that this throws Throwable directly? Shouldn't make much difference but semantically it'd make sense to be able to catch Errors and the likes if testing code that could raise errors directly, which I assume this would do already.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, it wouldn't catch Throwable or Error instances as the input parameter is a Callable, and Callable::call allows throwing Exceptions only.

Maybe we should consider if a new Callable-like interface that throws Throwable should be defined instead of relying on the JDK Callable.

@ascopes do you already have a concrete use case that would require it?

@scordio scordio Jan 2, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested by @sbrannen in #3314 (comment), a ThrowingSupplier could be defined if we decide that a Callable throwing Exception is not flexible enough.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assert return value of callable throwing exception

3 participants