Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertThatThrownBy.doesNotThrowAnyException() fails when no exception is thrown #2517

Open
TheBestPessimist opened this issue Mar 7, 2022 · 9 comments
Assignees

Comments

@TheBestPessimist
Copy link

Summary

Duplicate of #1787 because that is closed:

Example

    fun thisFunctionDoesNothing() {
        println("i did nothing")
    }

    @Test
    fun `this test should not fail, unfortunately it does`() {
        assertThatThrownBy { thisFunctionDoesNothing() }.doesNotThrowAnyException()
    }

And i get the exception:

Expecting code to raise a throwable.
java.lang.AssertionError: 
Expecting code to raise a throwable.
@TheBestPessimist TheBestPessimist changed the title assertThatThrownBy doesNotThrowAnyException fail when callable doesnt throw any exception assertThatThrownBy.doesNotThrowAnyException() fails when no exception is thrown Mar 7, 2022
@onacit
Copy link
Contributor

onacit commented Mar 12, 2022

You already have the comment.

I think the failure came from the assertThatThrownBy not from the doesNotThrowAnyException.

In code,

        Assertions.assertThatThrownBy(() -> { // expects this part throw any exception
                    thisFunctionDoesNothing();
                })
                .doesNotThrowAnyException();

You should've done like,

        Assertions.assertThatCode(() -> {
                    thisFunctionDoesNothing();
                })
                .doesNotThrowAnyException();

@TheBestPessimist
Copy link
Author

TheBestPessimist commented Mar 12, 2022

That's true and i used assertThatCode, however my issue is that doesNotThrowAnyException should be removed from assertThatThrownBy in this case. It leads to unexpected results.

I'm sorry for not explaining that in the OP.

@joel-costigliola
Copy link
Member

joel-costigliola commented Mar 12, 2022

I agree doesNotThrowAnyException should not be available after calling assertThatThrownBy.
doesNotThrowAnyException is meant to use with assertThatCode as pointed out by @onacit, when we added it we did not realize it would be possible to use it after assertThatThrownBy.

Having said that, we think it is natural for assertThatThrownBy to expect an exception to be thrown (that's the point of this method) so we won't change assertThatThrownBy behavior that fails when none was thrown.

@TheBestPessimist
Copy link
Author

TheBestPessimist commented Mar 13, 2022

doesNotThrowAnyException should not be available after calling assertThatThrownBy

as long as this happens, i'm happy!

at least, I don't have to explain this to any new colleague joining my team.

@hezean
Copy link
Contributor

hezean commented Mar 14, 2022

I agree doesNotThrowAnyException should not be available after calling assertThatThrownBy

Seems to be a great feature! If possible, I'd like to do some investigation and try to work on it 😄

@joel-costigliola
Copy link
Member

We are already looking at it @hezean but thanks!

@pandoras-toolbox
Copy link

pandoras-toolbox commented Nov 1, 2023

What I miss is this feature to be able to ignore certain exceptions:

 Assertions.assertThatCode(() -> {
                    thisFunctionDoesNothing();
                })
                .doesNotThrowAnyException()
                .ignoring(NoSuchElementException.class);

What do you think about this idea?

@joel-costigliola
Copy link
Member

@pandoras-toolbox yep why not ? I would probably go with doesNotThrowAnyExceptionExcept(Class... exceptions), ex:

assertThatCode(() -> { thisFunctionDoesNothing(); })
                .doesNotThrowAnyExceptionExcept(NoSuchElementException.class, IOException.class);

could you create a new issue for this?

@pandoras-toolbox
Copy link

Great! I created it.

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

No branches or pull requests

6 participants