Mockito 中的存根默认值

发布于 2024-10-03 04:01:13 字数 599 浏览 5 评论 0原文

如何存根一个方法,以便在给定一个我不期望的值时,它返回一个默认值?

例如:

Map<String, String> map = mock(Map.class);
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");
when(map.get(anyString())).thenReturn("I don't know that string");

第 2 部分:如上所述,但抛出异常:

Map<String, String> map = mock(Map.class);
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");
when(map.get(anyString())).thenThrow(new IllegalArgumentException("I don't know that string"));

在上面的示例中,最后一个存根优先,因此映射将始终返回默认值。

How can I stub a method such that when given a value I'm not expecting, it returns a default value?

For example:

Map<String, String> map = mock(Map.class);
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");
when(map.get(anyString())).thenReturn("I don't know that string");

Part 2: As above but throws an exception:

Map<String, String> map = mock(Map.class);
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");
when(map.get(anyString())).thenThrow(new IllegalArgumentException("I don't know that string"));

In the above examples, the last stub takes precedence so the map will always return the default.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对岸观火 2024-10-10 04:01:13

我发现的最好的解决方案是反转存根的顺序:

Map<String, String> map = mock(Map.class);
when(map.get(anyString())).thenReturn("I don't know that string");
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");

当默认抛出异常时,您可以只使用 doThrow 和 doReturn

doThrow(new RuntimeException()).when(map).get(anyString());
doReturn("defg").when(map).get("abcd");
doReturn("ghij").when(map).get("defg");

https://static.javadoc.io/org.mockito/mockito-core/2.18.3/org/mockito/Mockito .html#12

The best solution I have found is to reverse the order of the stubs:

Map<String, String> map = mock(Map.class);
when(map.get(anyString())).thenReturn("I don't know that string");
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");

When the default is to throw an exception you can just use doThrow and doReturn

doThrow(new RuntimeException()).when(map).get(anyString());
doReturn("defg").when(map).get("abcd");
doReturn("ghij").when(map).get("defg");

https://static.javadoc.io/org.mockito/mockito-core/2.18.3/org/mockito/Mockito.html#12

枫以 2024-10-10 04:01:13
when(map.get(anyString())).thenAnswer(new Answer<String>() {
    public String answer(Invocation invocation) {
        String arg = (String) invocation.getArguments()[0];
        if (args.equals("abcd")
             return "defg";
        // etc.
        else
             return "default";
             // or throw new Exception()
    }
});

这是一种迂回的方式来做到这一点。但它应该有效。

when(map.get(anyString())).thenAnswer(new Answer<String>() {
    public String answer(Invocation invocation) {
        String arg = (String) invocation.getArguments()[0];
        if (args.equals("abcd")
             return "defg";
        // etc.
        else
             return "default";
             // or throw new Exception()
    }
});

It's kind of a roundabout way to do this. But it should work.

明媚如初 2024-10-10 04:01:13

您可以使用:

Map<String, String> map = mock(Map.class, new Returns("I don't know that string"));
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");

You can use:

Map<String, String> map = mock(Map.class, new Returns("I don't know that string"));
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文