0

I'm actually not sure if this is an IntelliJ thing or a Java thing.

I made a utility method that asserts that a specific type of exception is thrown:

  public static <T, U extends Throwable> Throwable assertThrowsWithCause(
      Class<T> expectedOuterExceptionType,
      Class<U> expectedInnerExceptionType,
      Executable executable) {
    Exception outerException = assertThrows(Exception.class, executable);
    assertEquals(expectedOuterExceptionType, outerException.getClass());
    assertEquals(expectedInnerExceptionType, outerException.getCause().getClass());
    return outerException.getCause();
  }

It's used like this:

  @Test
  public void getMeasurementsConfig_configDoesNotExist() {
    when(configManager.getConfig(sampleUUID.toString()))
        .thenReturn(CompletableFuture.failedFuture(new NotFoundException("")));
    assertThrowsWithCause(
        CompletionException.class,
        NotFoundException.class,
        () -> configController
            .getMeasurementsConfig(Context.ROOT, sampleRequest)
            .toCompletableFuture().join());
  }

But there is a warning on the method call:

Result of 'assertThrowsWithCause()' not thrown

Which I've learned I can suppress by adding

  @Test
  @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
  public void ...

on the calling method. But that means I'd have to add this to every single test across many files. How can I suppress this warning globally, in such a way it would be suppressed for everyone working on the project, i.e. not just in my local IntelliJ Settings? I tried suppressing on the called method, i.e.

  @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
  public static <T, U extends Throwable> Throwable assertThrowsWithCause(

but this did not have the same effect.

Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
  • https://www.jetbrains.com/help/idea/configuring-inspection-severities.html – OH GOD SPIDERS Aug 07 '23 at 09:24
  • Why does `assertThrowsWithCause()` returns the cause at all?? – tquadrat Aug 07 '23 at 09:30
  • @tquadrat - That is what what org.junit.jupiter.api.assertThrows does. I was just modeling my utility the same way as theirs. I think they return it because in a test you may not only want to test the type of the exception but assert more things about its properties. Anyway, not my invention. – Andrew Cheong Aug 07 '23 at 14:23

1 Answers1

0

This is not a warning from javac, but an IntelliJ Inspection. The name of the inspection is "'Throwable' not thrown".

You can find the name of an inspection by using the context actions menu (the light bulb that appears), and then clicking on the three dots next to the relevant context action:

enter image description here

To disable it project-wide, just search for the name in Settings and uncheck the checkbox:

enter image description here

In the case of a warning from javac, you can pass a -Xlint option to javac (see this answer).

Sweeper
  • 213,210
  • 22
  • 193
  • 313