有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

模拟期望集合的方法时发出java JMockit警告

有没有一种方法可以让以下模拟在没有未检查强制类型转换警告的情况下工作:

new Expectations() {{
        UrlService.addUrls((List<String>)any); result = expectedCandidates; 
}};

UrlService.addUrls()方法的签名是:

static List<Candidate> addUrls(List<String> urls)

共 (2) 个答案

  1. # 1 楼答案

    最好的替代方法是使用T witnAny(T arg)参数匹配器:

    new Expectations() {{
        UrlService.addUrls(withAny(new ArrayList<String>()));
        result = expectedCandidates;
    }};
    

    或者,如果IDE支持,在本地禁用代码检查。有了IntelliJ,我可以写:

    new Expectations() {{
        //noinspection unchecked
        UrlService.addUrls((List<String>) any);
        result = expectedCandidates;
    }};
    

    。。。这真的没关系。代码检查是很好的,但总有一些例外情况,可以禁用它们

  2. # 2 楼答案

    试试这个:

    new Expectations() {
            {
                UrlService.addUrls(withArgThat(new IsAnything<List<String>>())); result = expectedCandidates;
            }
        };