开发者

How stub instanseof in Mockito in java?

I have method that accept argument of Object type. But inside of method checked if is e.g. List type. Is possible in mockito to stub it? E.g.

public void checkValue(Object arg) {
   if (arg instanceof List) {
    ....

So in mockito:

开发者_如何学JAVA
Object myObject=mock(Object.class);

After I need write something like:

when (myObject instanceof List).thenReturn List

How it can be done? Thanks.


Sure, you can mock how ever you want. Ex:

Object o = mock(List.class);


There is an advice that states

Do not mock types you don't own

So your test should accept an instance of a real list object new ArrayList() instead of a mock.

Should you only mock types you own?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜