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

Aleksey Shipilev shade at openjdk.java.net
Tue Mar 9 20:55:15 UTC 2021


On Tue, 9 Mar 2021 19:58:26 GMT, Henri Tremblay <github.com+1216819+henri-tremblay 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
> }

True, except that JMH codebase is still at JDK 7 compatibility, so no nice lambdas, and this gets much less appealing.

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

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


More information about the jmh-dev mailing list