使用scala使用Mockito传递匹配器和值

时间:2016-07-08 20:04:43

标签: scala mockito

我的测试中有以下代码......

val list : List[MyClass] = List(new MyClass)
...
doNothing().when(localLib).handleDelete(anyString(), anyString(), anyInt(), eq(list))

这会引发以下编译异常

Error:(890, 83) type mismatch;
 found   : Boolean
 required: java.util.List[my.package.MyClass]
    doNothing().when(localLib).handleDelete(anyString(), anyString(), anyInt(), eq(list))
                                                                                  ^

2 个答案:

答案 0 :(得分:2)

您必须使用scalatest吗?与Mockito存在冲突,其中一个*Spec特征定义了自己的eq,您最终会引用该Mockito匹配而不是impot org.mockito.Matcher.{ eq => mockEq }匹配器。

尝试添加显式导入:mockEq 然后在需要匹配器时使用eq代替function getStatData($y,$m,$d=false){ if(isset($y) && is_numeric($y) && isset($m) && is_numeric($m)){ // use str_pad $m=str_pad($m, 2, '0', STR_PAD_LEFT); if($d){ // use str_pad $d=str_pad($d, 2, '0', STR_PAD_LEFT); $y= 'days/'.$y; $data = file_get_contents(STATDIR.'/'.$y.'/'.$m.'_'.$d.'.json'); } else { $data = file_get_contents(STATDIR.'/'.$y.'_'.$m.'.json'); } return json_decode($data, true); } else return false; } print_r(getStatData('2016','07','08'));

答案 1 :(得分:0)

eq(list)确实是布尔值,检查签名。你应该嘲笑最后一个论点:

doNothing().when(localLib)
  .handleDelete(anyString(), anyString(), anyInt(), any[List[MyClass]])