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

4.8.0 RV_EXCEPTION_NOT_THROWN False positive with JUnit 5 assertThrows #2628

Closed
josephearl opened this issue Oct 13, 2023 · 5 comments · Fixed by #2648
Closed

4.8.0 RV_EXCEPTION_NOT_THROWN False positive with JUnit 5 assertThrows #2628

josephearl opened this issue Oct 13, 2023 · 5 comments · Fixed by #2648

Comments

@josephearl
Copy link

RV | org.junit.jupiter.api.Assertions.assertThrows(Class, Executable) not thrown in com.my.package.SomeServiceTest$someNestedTest.itShouldDoSomething()

Called method org.junit.jupiter.api.Assertions.assertThrows(Class, Executable)
At SomeServiceTest.java:[line 85]

Where line 85 is:

assertThrows(SomeException.class, () -> someService.doSomething("someParam"));
@josephearl josephearl changed the title 4.8.0 RV_EXCEPTION_NOT_THROWN False positive with AssertJ assertThrows 4.8.0 RV_EXCEPTION_NOT_THROWN False positive with JUnit 5 assertThrows Oct 13, 2023
@JuditKnoll
Copy link
Collaborator

Can you please provide the someService.doSomething("someParam") function, or at least the header?
I think it got into the code when fixing #2547.

@josephearl
Copy link
Author

josephearl commented Oct 13, 2023

The method signature is:

public void doSomething(String param)

SomeException is a runtime exception

@bwmeier
Copy link

bwmeier commented Oct 16, 2023

This is flagging just about all of our JUnit tests for exception behavior. It shows up with both org.junit.jupiter.api.Assertions#assertThrows(java.lang.Class<T>, org.junit.jupiter.api.function.Executable) and org.junit.Assert#assertThrows(java.lang.Class<T>, org.junit.function.ThrowingRunnable) invocations, and both Java standard and custom exceptions.

@bwmeier
Copy link

bwmeier commented Oct 16, 2023

As an additional note, I changed one of the tests to use the AssertJ method org.assertj.core.api.Assertions#assertThatExceptionOfType, and the spotbugs failure went away.

@bwmeier
Copy link

bwmeier commented Oct 16, 2023

I think I have a bit more information here. assertThrows returns the exception for further testing by the unit test. If the unit test does nothing with the returned value, then the error is raised by spotbugs. If the unit test does further work with the returned exception value, then the error is not raised. Basically the following code raises the spotbugs error:

Assertions.assertThrows(NullPointerException.class, () -> methodThrowingNpe(null));

but the following code does not:

var npe = Assertions.assertThrows(NullPointerException.class, () -> methodThrowingNpe(null));
Assertions.assertTrue(npe.getMessage().contains("something"));

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 a pull request may close this issue.

3 participants