0

Example:

public T f1() {
  try{
    Object o = new Object();
    
    o.someFunc() // i want to mock this function call to throw an exception (ex) Exception1)

  }
  catch (Exception1 e) {
      throw new Exception2() 
 }

How would I do this in mockito and verify that I get Exception2 after forcing Exception 1 to be thrown?

  • Does this answer your question? [Mockito How to mock and assert a thrown exception?](https://stackoverflow.com/questions/16243580/mockito-how-to-mock-and-assert-a-thrown-exception) – Amon Jul 19 '21 at 18:59

1 Answers1

0

You can use when and thenThrow to mocke Exeption1 from someFunc

when(someService.someFunction("param1", "param12")).thenThrow( new Exception1());

then f1() should catch exception and throw Exception2

you can assert exception class Name as below,

assertEquals(mvcResult.getResolvedException().getClass().getName(), "org.springframework.web.bind.MissingServletRequestParameterException");
sanjeevRm
  • 1,541
  • 2
  • 13
  • 24