1

I wrote a following test case. ImprovedAlternateIterator is a custom iterator that can accept multiple iterator in its constructor. I wrote a test case to check its hasNext function. I am sure that it will throw an error but I don't know how to handle it. In junit4 we can use @Test(expected = exception.class) and its not possible in junit5.

I followed the documents and wrote the following test case with junit5 but it failed.

@Test
    public void hasNextTestWithEmptyConstructor() 
    ImprovedAlternateIterator <E> alternatingIterator = new ImprovedAlternateIterator<E>();
    assertThrows(ArrayIndexOutOfBoundsException.class, () ->
    alternatingIterator.hasNext()); 
}

How can I handle the exception?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
s.p
  • 129
  • 2
  • 4
  • 10
  • 1
    How exactly does it fail? What is the error/exception message you get? What do you expect instead? – Progman Oct 13 '18 at 22:34
  • 2
    Possible duplicate of [JUnit 5: How to assert an exception is thrown?](https://stackoverflow.com/questions/40268446/junit-5-how-to-assert-an-exception-is-thrown) – Juliano Macedo Oct 13 '18 at 22:41
  • @Progman it failed the test. no error message shown. I just want to handle the arrayindexoutofbound error – s.p Oct 13 '18 at 22:45

0 Answers0