【问题标题】:Mockito Junit 5 Throw checked exception not workingMockito Junit 5 Throw 检查异常不起作用
【发布时间】:2021-03-03 17:24:23
【问题描述】:

我试图在方法调用期间抛出 SQLException。但是由于某种原因没有抛出异常。

错误信息

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)

我希望在调用 dropRun() 时抛出异常

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

它的实现

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

我想测试的类

public interface Project {

  void purge() throws SQLException;

}

及其实现

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;
    }
  }
}

测试类

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());
  }

}

我尝试了几个链接,但没有运气!任何帮助将不胜感激。 TIA。

Link1 Link2

【问题讨论】:

  • 您能否提供一个完整的代码来帮助您?我无法弄清楚变量 - ingestRunRepository
  • 看来你的ingestRunRepository和模拟对象runRepository不一样
  • @Lokesh 抱歉打错了。我已经更新了问题
  • 此时您提到的willAnswer 的语法似乎无效。您是否尝试过此线程中显示的不同示例? stackoverflow.com/questions/3762047/… with-mockito?noredirect=1&lq=1 如果您能更新完整代码,我将不胜感激。至少测试所有导入的类。
  • 我重建了您的示例并为我工作,包括 Trail 1 和 2。您使用的是什么 mockito 版本?

标签: java mockito junit5


【解决方案1】:

我面临同样的问题,以下代码不起作用。 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));

}

但如果我将 CheckedExcpetion 替换为 RunTime 异常,它会按预期工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多