Skip to content

Migrate remaining test files to use getExpectedThrowable instead of fail() #19176

@ayushactiveat

Description

@ayushactiveat

we recently introduced a new Checkstyle rule in pr #19168 that forbids using the old assertWithMessage(...).fail() pattern inside try-catch blocks. Instead, we want to use the cleaner getExpectedThrowable utility.

We already updated a few packages, but there are still around 240 test files across the codebase that need to be migrated.

how to find the files that need updating->
you can get a list of every file and line number that needs work by running - grep -rn "assertWithMessage.*fail()"

So, what needs to be changed-
you will see old code that looks like->

try {
    someMethodThatThrowsException();
    assertWithMessage("Exception is expected").fail();
}
catch (IllegalArgumentException ex) {
    assertWithMessage("Invalid exception message")
        .that(ex.getMessage())
        .isEqualTo("expected message");
}

update it to look like -

final IllegalArgumentException ex = getExpectedThrowable(IllegalArgumentException.class, () -> {
    someMethodThatThrowsException();
});
assertWithMessage("Invalid exception message")
    .that(ex.getMessage())
    .isEqualTo("expected message");

also this is a large task, please do not try to do all 240+ files in one pr
pick a specific package/small group of files (5-10 file).

comment below "i am working on xxxx files/package" so others don't work on the same ones

after the fix to ensure your changes are actually tested by the new rule, you must update the suppression list. Open config/suppressions.xml and find the MatchXpath suppression. It's a one liner using regex:
files=".*[\\/]...[\\/](?!utils|xpath).*"
so if you fixed the api package, you would update that line to:
files=".*[\\/]...[\\/](?!utils|xpath|api).*"

example of this migration - #19168

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions