Skip to content

assertThrows doesn't fail if the thrown exception is of child type #2505

@govi20

Description

@govi20

Steps to reproduce

assertThrows(IOException.class, () -> service.readFile("sampleFile.txt"));

this assertion will pass even if the exception is FileNotFoundException.

I believe this is happening because we are using Class#isInstance it will return true for parent types also.

https://github.com/junit-team/junit5/blob/637a78393194737e77d6f9681ad93e9ddac1f399/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertThrows.java#L58

Shouldn't this be replaced with if (expectedType == actualException.getClass()) ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions