简体   繁体   English

Mockito Junit 5 抛出检查异常不起作用

[英]Mockito Junit 5 Throw checked exception not working

I am trying to throw SQLException during a method call.我试图在方法调用期间抛出 SQLException。 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我希望在调用 dropRun() 时抛出异常

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. TIA。

Link1 Link2 链接1链路2

I am facing the same issue, The following code doens't work.我面临同样的问题,以下代码不起作用。 JUnit fails with JUnit 失败

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但是,如果我用运行时异常替换 CheckedExcpetion,它会按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM