簡體   English   中英

Mockito Junit 5 拋出檢查異常不起作用

[英]Mockito Junit 5 Throw checked exception not working

我試圖在方法調用期間拋出 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。

鏈接1鏈路2

我面臨同樣的問題,以下代碼不起作用。 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,它會按預期工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM