Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

I'm trying to have one of my mocked objects throw a checked Exception when a particular method is called. I'm trying the following.

@Test(expectedExceptions = SomeException.class)

public void throwCheckedException() {

    List<String> list = mock(List.class);

    when(list.get(0)).thenThrow(new SomeException());

    String test = list.get(0);

}

public class SomeException extends Exception {

}

However, that produces the following error.

org.testng.TestException: 

Expected exception com.testing.MockitoCheckedExceptions$SomeException but got org.mockito.exceptions.base.MockitoException: 

Checked exception is invalid for this method!

Invalid: com.testing.MockitoCheckedExceptions$SomeException

Looking at the Mockito documentation, they only use RuntimeException, is it not possible to throw checked Exceptions from a mock object with Mockito?

1 Answer

0 votes
by (46k points)

Check the Java API for List. The get(int) method is declared to throw only the IndexOutOfBoundException which extends RuntimeException. You are trying to tell Mockito to a throw an exception that is not valid to be thrown by that particular method call.

To clarify further. The List interface does not provide for a checked Exception to be thrown from the get() method and that is why Mockito is failing. When you create the mocked List, Mockito using the definition of List.class to creates its mock. The behavior you are specifying with the when(list.get(0)).thenThrow(new SomeException()) doesn't match the method signature in List.class, so Mockito fails. If you really want to do this, then have Mockito throw a new RuntimeException() or even better throw a new ArrayIndexOutOfBoundsException() since the API specifies that that is the only valid Exception to be thrown.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...