使用 免费vpn Mockito 从模拟中抛出已检查的异常

2022-11-01Java开发问题
635

vpn free 本文介绍了使用 Mockito 从模拟中抛出已检查的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! 免费vpn下载 vpn free 免费vpn

问题描述

我试图让我的一个模拟对象在调用特定方法时抛出一个检查异常.我正在尝试以下方法.

I'm trying to have one of my mocked objects throw vpn下载 a checked Exception when a particular method is called. I'm trying the following.

@Test(expectedExceptions = SomeException.class)
public void throwCheckedException() {
  免费vpn下载   List<String> list = mock(List.class);
    when(list.get(0)).thenThrow(new SomeException());
 免费vpn   免费vpn下载 免费vpn  String test = list.get(0);
}

public class SomeException extends Exception {
}

但是,这会产生以下错误.

However, that produces the following error.

org.testng.TestException: 
Expected exception com.testing.MockitoCheckedExceptions$SomeException but got org.mockito.exceptions.base.MockitoException: 
Checked exception is invalid for this method!
Invalid: com.testing.MockitoCheckedExceptions$SomeException

查看 Mockito 文档,他们只使用RuntimeException,难道不能用Mockito从一个mock对象中抛出checked Exceptions吗?

Looking at free vpn the Mockito documentation, they only use RuntimeException, is vpn下载 it not 免费vpn下载 possible to throw checked Exceptions from vpn下载 a mock object with Mockito?

推荐答案

检查 列表.
get(int index) 方法被声明为仅抛出扩展 RuntimeExceptionIndexOutOfBoundException.
您正试图告诉 Mockito 抛出一个异常 vpn free SomeException(),该异常 该特定方法调用无法抛出该异常.

Check the Java API for List.
The get(int vpn下载 index) method is declared to vpn下载 throw only the IndexOutOfBoundException which extends RuntimeException.
You are trying to tell free vpn 免费vpn Mockito to throw an exception SomeException() that 免费vpn is not valid to vpn free be thrown by that particular method call.

进一步澄清.
List 接口不提供从 get(int index) 方法抛出一个经过检查的异常,这就是 Mockito 免费vpn 失败的原因.
当您创建 mocked List 时,Mockito 将使用 List 的定义.class 来创建它的 mock.

To clarify further.
The List interface does not provide for a checked vpn下载 Exception to be thrown from the get(int index) method and that is why Mockito is failing.
When vpn free you create the mocked List, Mockito 免费vpn下载 will use the definition 免费vpn下载 of List.class to creates its mock.

您使用 vpn下载 when(list.get(0)).thenThrow(new SomeException()) 指定的行为 与 List API vpn下载 中的方法签名不匹配,因为 get(int index) 方法不会抛出 SomeException() 所以 Mockito 失败.

The behavior you are specifying with the when(list.get(0)).thenThrow(new SomeException()) doesn't match the method signature in List API, free vpn because vpn free get(int index) 免费vpn method does not throw SomeException() so Mockito fails.

如果你真的想这样做,那么让 Mockito 抛出一个 new RuntimeException() 甚至更好地抛出一个 new ArrayIndexOutOfBoundsException() 因为 API 指定这是要抛出的唯一有效异常.

If you really want to vpn下载 do this, then have Mockito vpn free throw a new RuntimeException() or even better throw a new ArrayIndexOutOfBoundsException() since the API specifies that that is the only valid Exception to be thrown.

这篇关于使用 Mockito 免费vpn 从模拟中抛出已检查的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send free vpn data to COM PORT using JAVA?(如何使用 vpn free JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to vpn下载 change vpn free to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 vpn free .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle 免费vpn messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default free vpn locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13