javaseleniumjunit5asserttimeoutexception

Why am I facing issue with junit 5 assertThrows for asserting TimedOutException despite passing expected parameters?


I'm trying to assert the TimedOutException using junit 5 assertThrows while clicking a button on page but I'm getting error, may I know the reason?enter image description here

Assert.assertThrows(TimedOutException.class, ()->{homePage.clickQuote();});

clickQuote() method either takes user to next page or throws TimedOutException.


Solution

  • You probably imported and used the wrong class (from JUnit 4 instead of JUnit 5)

    In JUnit 5 please use only org.junit.jupiter.api.Assertions instead of org.junit.Assert.

    As you mentioned in a comment that you imported org.unit.Assert: This is the utility with the static assertions from JUnit 4, not JUnit 5. It provides a method assertThrows, but with different parameters, and is not upwards compatible with JUnit 5. Usually you expect exceptions in JUnit 4 with the help of annotations.

    You can find details on the method parameters in the API docs of JUnit 5.