Skip to content

Avoid assertTrue() or assertFalse() with constant boolean arguments #762

@greg-at-moderne

Description

@greg-at-moderne

What problem are you trying to solve?

Test code readability.

In a test case:

  • assertTrue(true) is a no-op
  • assertTrue(false) can be replaced with fail()

Similar for assertFalse, etc. The same logic could probably be done in several variants for various test frameworks.

Describe the situation before applying the recipe

    @Test
    void testExceptionThrown() {
        try {
            someOperationWith("Invalid argument");
            fail("Expected exception was not thrown");
        } catch (IllegalArgumentException e) {
            assertTrue(true);             
        }
    }

Describe the situation after applying the recipe

    @Test
    void testExceptionThrown() {
        try {
            someOperationWith("Invalid argument");
            fail("Expected exception was not thrown");
        } catch (IllegalArgumentException e) {
        }
    }

Any additional context

I've stumbled upon this pattern when providing OSS examples for

Metadata

Metadata

Assignees

No one assigned

    Labels

    recipeRecipe request

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions