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