1

I am trying to throw SQLException during a method call. But the exception is not thrown for some reason.

Error message

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)

I would like the exception to be thrown when dropRun() is invoked

public interface RunRepository {
  void dropRun(List<Integer> ids) throws SQLException;
}

Its Implementation

public class RunRepositoryImpl implements RunRepository{
    @Override
  public void dropRun(List<Integer> ids) throws SQLException {
    //some piece of code
  }
}

The Class I would like to test

public interface Project {

  void purge() throws SQLException;

}

and its Implementation

public ProjectImpl implements Project{
  @Override
  public void purge() throws SQLException {
    //some piece of code
    try {
      runRepository.dropRun(ids);
    } catch (Exception e) {
      LOGGER.error("Error purging completed runs.");
      throw e;
    }
  }
}

Test class

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 -> {
      throw new SQLException();
    }).given(runRepository).dropRun(any());


    //Then
    assertThrows(SQLException.class, () -> project.purge());
  }

}

I tried a couple of links, but no luck!. Any help would be highly appreciated. TIA.

Link1 Link2

Andy
  • 5,433
  • 6
  • 31
  • 38
  • 1
    could you please provide a full code to help you out? I cannot figure out about variable - ingestRunRepository – Lokesh Oct 25 '20 at 02:56
  • 1
    It seems your `ingestRunRepository` is not same as the mock object `runRepository` – haoyu wang Oct 25 '20 at 03:06
  • @Lokesh sorry for the typo. I have updated the question – Andy Oct 25 '20 at 04:14
  • The syntax of willAnswer you have mentioned at this point in time seems to be invalid. Have you tried different examples shown in this thread? https://stackoverflow.com/questions/3762047/throw-checked-exceptions-from-mocks- with-mockito?noredirect=1&lq=1 I appreciate if you can update with full code. at least test class with all imports. – Lokesh Oct 25 '20 at 04:52
  • @Lokesh I have included the imports as well. Thanks. – Andy Oct 25 '20 at 05:45
  • I tried the examples mentioned in this answer and in its comments, but no luck! https://stackoverflow.com/a/48261005/2353713 – Andy Oct 25 '20 at 06:40
  • 1
    I rebuilt your example and works for me, both Trail 1 and 2. What mockito version are you using? – fonkap Oct 25 '20 at 08:26
  • org.mockito:mockito-core:jar:3.3.3:test org.mockito:mockito-junit-jupiter:jar:3.3.3:test May I please know the version you are using? – Andy Oct 26 '20 at 02:43
  • @fonkap Can you please let me know the version of mockito you are using? – Andy Oct 28 '20 at 04:02
  • I used 3.1.0 but I tried 3.3.3 with the same results. Let me share my project: https://github.com/fonkap/so_64519322 – fonkap Oct 29 '20 at 18:52
  • @fonkap Thanks. I will have a look at your code and compare it with mine. I will let you know how it goes. Thanks again – Andy Oct 29 '20 at 20:25

1 Answers1

1

I am facing the same issue, The following code doens't work. JUnit fails with

org.mockito.exceptions.base.MockitoException: 
Checked exception is invalid for this method!
Invalid: javax.xml.bind.JAXBException: aa
DocumentService documentService = mock(DocumentService.class);

 @Test
 @DisplayName("Returns 500 http status when we have an error calling the PFEL")
 void handle_document_create_request_exception_in_service() {

 willThrow(new JAXBException("aa")).given(documentService).generateDocument(any(DocumentCreateDto.class));

}

But if I replace the CheckedExcpetion with a RunTime exception, it works as expcected

razvang
  • 1,055
  • 11
  • 15