Skip to content

Wrap assertThrows with getExpectedThrowable method. #11343

@nrmancuso

Description

@nrmancuso

From #11315 (comment):

ATTENTION: I do not know why, but my brain do not not reading assertThrows properly. as it should be named expectThrows and assert on it done in separate block of code. should we create util method in our repo? to wrap/re-call assertThrows. To just have different name of method that is more logical to my mind.

code will looks like:

    public void testMethodGood() {

        int[] arr = new int[]{};
        final IndexOutOfBoundsException exception =
                expectThrows(IndexOutOfBoundsException.class,
                        () -> {
                            int i = arr[2];
                        });

        assertWithMessage("Invalid error message")
                .that(exception.getMessage())
                .isEqualTo("Index 2 out of bounds for length 0");

    }

In the scope of this issue, we should:

  1. Create getExpectedThrowablemethod in TestUtils
  2. Restrict import of Assertions#assertThrows to TestUtils only
  3. Fix all violations in project.
  4. UpdateMatchXPath module introduced in Forbid usage of Truth's StandardSubjectBuilder#fail method. #11315

We can consider using new feature mentioned at #11995 (review)

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