title: Mockito Junit 5 Throw checked exception not working
body: <p>I am trying to throw SQLException during a method call. But the exception is not thrown for some reason.</p>
  <p>Error message</p>
  <pre><code>org.opentest4j.AssertionFailedError: Expected java.sql.SQLException to be thrown, but nothing was thrown.
  
      at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:71)
      at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:37)
      at org.junit.jupiter.api.Assertions.assertThrows(Assertions.java:2952)
  </code></pre>
  <p>I would like the exception to be thrown when dropRun() is invoked</p>
  <pre><code>public interface RunRepository {
    void dropRun(List&lt;Integer&gt; ids) throws SQLException;
  }
  </code></pre>
  <p>Its Implementation</p>
  <pre><code>public class RunRepositoryImpl implements RunRepository{
      @Override
    public void dropRun(List&lt;Integer&gt; ids) throws SQLException {
      //some piece of code
    }
  }
  </code></pre>
  <p>The Class I would like to test</p>
  <pre><code>public interface Project {
  
    void purge() throws SQLException;
  
  }
  </code></pre>
  <p>and its Implementation</p>
  <pre><code>public ProjectImpl implements Project{
    @Override
    public void purge() throws SQLException {
      //some piece of code
      try {
        runRepository.dropRun(ids);
      } catch (Exception e) {
        LOGGER.error(&quot;Error purging completed runs.&quot;);
        throw e;
      }
    }
  }
  </code></pre>
  <p>Test class</p>
  <pre><code>import static org.junit.jupiter.api.Assertions.assertThrows;
  import static org.mockito.ArgumentMatchers.any;
  import static org.mockito.BDDMockito.willAnswer;
  import static org.mockito.Mockito.mock;
  import static org.mockito.Mockito.never;
  import static org.mockito.Mockito.verify;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.kfj.repository.RunRepository;
  import com.kfj.service.Project;
  import java.sql.SQLException;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Disabled;
  import org.junit.jupiter.api.Test;
  import org.junit.jupiter.api.TestInstance;
  import org.junit.jupiter.api.TestInstance.Lifecycle;
  import org.junit.jupiter.api.extension.ExtendWith;
  import org.mockito.Mock;
  import org.mockito.junit.jupiter.MockitoExtension;
  @TestInstance(Lifecycle.PER_CLASS)
  @ExtendWith(MockitoExtension.class)
  public class ProjectImplTest {
  
    private Project project;
  
    @Mock
    private RunRepository runRepository;
  
    @BeforeEach
    public void setUp() {
      //some piece of code
      project = new ProjectImpl(runRepository);
    }
      
    @Test
    public void GIVEN_non_empty_completed_runs_WHEN_purge_is_invoked_THEN_dropRun_is_invoked()
        throws SQLException {
  
      //MOCK Trail 1 DIDN'T WORK
      //doThrow(SQLException.class).when(runRepository).dropRun(any());
  
      //MOCK Trail 2 DIDN'T WORK either
      willAnswer(invocation -&gt; {
        throw new SQLException();
      }).given(runRepository).dropRun(any());
  
  
      //Then
      assertThrows(SQLException.class, () -&gt; project.purge());
    }
  
  }
  </code></pre>
  <p>I tried a couple of links, but no luck!. Any help would be highly appreciated. TIA.</p>
  <p><a href="https://stackoverflow.com/questions/3762047/throw-checked-exceptions-from-mocks-with-mockito">Link1</a>
  <a href="https://www.baeldung.com/mockito-exceptions" rel="nofollow noreferrer">Link2</a></p>