【问题标题】:Hamcrest matcher for collection argument without cast没有强制转换的集合参数的 Hamcrest 匹配器
【发布时间】:2018-03-20 10:38:22
【问题描述】:

我有一个类型:

class SomeType {}

我有一种方法可以用MockitoHamcrest 模拟:

class SomeService {
    public List<SomeType> doSomething(List<SomeType> producer) {
        ...
    }
}

如果传递了java.util.List 并且该列表包含至少一个指定元素,我需要模拟doSomething() 方法。 现在我使用强制转换来编译:

when(someService.doSomething(
    ((List<SomeType>) MockitoHamcrest.<List<SomeType>>argThat(CoreMatchers.hasItem(mySomeType))))
.thenReturn(emptyList());

但我想让它在没有强制转换的情况下输入安全。有没有办法进行这样的检查?

【问题讨论】:

    标签: java mockito hamcrest


    【解决方案1】:

    抱歉,没有办法避免这种转换,即使您将生产方法参数更改为 Iterable&lt;SomeType&gt;,因为有人可能认为这可能会有所帮助,因为 argThat 返回一个 &lt;Iterable&lt;? super Object&gt;&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多