RFR: CODETOOLS-7902838: JMH: Don't use fail() inside a try-catch catching an AssertionError

Henri Tremblay github.com+1216819+henri-tremblay at openjdk.java.net
Tue Mar 9 20:01:14 UTC 2021


On Tue, 9 Mar 2021 13:59:19 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:

> SonarCloud instance reports a bug in our tests:
>  "Don't use fail() inside a try-catch catching an AssertionError."
> 
>     @Test
>     public void invokeAPI() throws RunnerException {
>         try {
>             ...
>             new Runner(opt).run();
> 
>             Assert.fail("Should have failed"); // <--- here
>         } catch (RunnerException e) {
>             // expected
>         }
>     }
> 
> Indeed, that does not look correct.

jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingBenchmarkBenchSetupTest.java line 76:

> 74:             new Runner(opt).run();
> 75: 
> 76:             Assert.fail("Should have failed");

If you want to have fun, with JUnit 4.13.2 (see #24) you can use `assertThrows`. This code will become

public void invokeAPI() throws RunnerException {
            Options opt = new OptionsBuilder()
                    .include(Fixtures.getTestMask(this.getClass()))
            RunnerException exception = assertThrows(() -> new Runner(opt).run());
            assertEquals(exception.getMessage(), "xxxxx"); // this is the bonus, checking it's the right message
}

-------------

PR: https://git.openjdk.java.net/jmh/pull/25


More information about the jmh-dev mailing list