Mockito:如何匹配任何枚举参数 [英] Mockito: How to match any enum parameter

查看:1214
本文介绍了Mockito:如何匹配任何枚举参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



private Long doThings(MyEnum enum,Long otherParam);
和这个枚举

  public enum MyEnum {
VAL_A,
VAL_B,
VAL_C
}

问题:我如何模拟 doThings() 来电?
我不能匹配任何 MyEnum



以下内容不起作用:

  Mockito.when(object.doThings(Matchers.any(),Matchers.anyLong()))
.thenReturn(123L);


解决方案

Matchers.any )会做的诀窍:

  Mockito.when(object.doThings(Matchers.any(MyEnum .class),Matchers.anyLong()))
.thenReturn(123L);

作为附注:考虑使用 Mockito 静态导入:

  import static org.mockito.Matchers。*; 
import static org.mockito.Mockito。*;

模拟得到的时间要短很多:

  when(object.doThings(any(MyEnum.class),anyLong()))。thenReturn(123L); 


I have this method declared like this

private Long doThings(MyEnum enum, Long otherParam); and this enum

public enum MyEnum{
  VAL_A,
  VAL_B,
  VAL_C
}

Question: How do I mock doThings() calls? I cannot match any MyEnum.

The following doesn't work:

Mockito.when(object.doThings(Matchers.any(), Matchers.anyLong()))
        .thenReturn(123L);

解决方案

Matchers.any(Class) will do the trick:

Mockito.when(object.doThings(Matchers.any(MyEnum.class), Matchers.anyLong()))
    .thenReturn(123L);

As a side note: consider using Mockito static imports:

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

Mocking gets a lot shorter:

when(object.doThings(any(MyEnum.class), anyLong())).thenReturn(123L);

这篇关于Mockito:如何匹配任何枚举参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆