Mockito:替换抛出异常的方法

Jor*_*rdi 4 java mockito

我有这个界面:

public interface IEnd<T> {
    T get() throws FluentApiException;
}

public interface IFluentEntityCollectionRelation<ID, T> extends IEnd<Iterable<T>>
Run Code Online (Sandbox Code Playgroud)

我试图模拟这个接口,以便在Iterable<T>调用时返回一个具体的空对象:

IFluentEntityCollectionRelation<String, Plan> plans = any();
when(plans.get()).thenReturn(new ArrayList<Plan>());
Run Code Online (Sandbox Code Playgroud)

尽管如此,我还是收到了编译错误:

未处理的异常类型 FluentApiException

因为IEnd<>.get()抛出了一个FluentApiException从未被捕获的异常。

有任何想法吗?

Gho*_*ica 6

简单:您只需要更改您的测试代码,例如

@Test
public void testWhatever() throws FluentApiException
Run Code Online (Sandbox Code Playgroud)

当然,测试用例永远不应该抛出那个异常。如果它被抛出,它将导致测试用例失败。

换句话说:你根本不在乎。使编译器中加入这个检查异常的测试方法(S)来调用开心get()