Mockito 学习篇(三)

Mockito学习篇目录:

Mockito 学习篇(一)

Mockito 学习篇(二)

Mockito 学习篇(三)

Mockito 学习篇(四)完结

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;

import static org.mockito.AdditionalAnswers.delegatesTo;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

/**
 * MockitoTester.
 *
 * @author zxb
 * @version 1.0
 * @since <pre>08/29/2017</pre>
 */
@RunWith(MockitoJUnitRunner.class)
public class MockitoTest3 {

    @Mock
    private LinkedList linkedList;

    @Spy
    private ArrayList arrayList;

    final class DontYouDareToMockMe extends ArrayList {

    }

    @Test
    public void test() throws InterruptedException {
        //区别一个对象是模拟对象还是侦查对象
        Assert.assertTrue(Mockito.mockingDetails(linkedList).isMock());
        Assert.assertTrue(Mockito.mockingDetails(arrayList).isSpy());
        //模拟对象不一定是侦查对象
        Assert.assertFalse(Mockito.mockingDetails(linkedList).isSpy());
        //侦查对象是模拟对象的一种类型
        Assert.assertTrue(Mockito.mockingDetails(arrayList).isMock());

        //委托调用真实实例(doCallRealMethod),用在以下几种场景:
        //Final classes but with an interface,有接口(有继承应该也行)的final类
        //Already custom proxied object ,已经自定义代理的对象
        //Special objects with a finalize method, i.e. to avoid executing it 2 times,final方法
        DontYouDareToMockMe dontYouDareToMockMe = new DontYouDareToMockMe();
        dontYouDareToMockMe.add(new HashSet());
        ArrayList listWithDelegate = mock(ArrayList.class, delegatesTo(dontYouDareToMockMe));
        Assert.assertEquals("mock对象的size不为1", 1, listWithDelegate.size());
        listWithDelegate.clear();
        //委托调用了真实方法,所以真实对象的size也为空了。
        Assert.assertEquals("dontYouDareToMockMe的size不为0", 0, dontYouDareToMockMe.size());
        //因为是调用真实对象的方法,所以当索引不存在时,不能直接用when..thenReturn方法
        //when(listWithDelegate.get(0)).thenReturn("foo");
        doReturn("foo").when(listWithDelegate).get(0);
        Assert.assertEquals("foo", listWithDelegate.get(0));
     //比如对String类mock
     String str = "vvv";
     CharSequence mock = mock(CharSequence.class, delegatesTo(str));
     System.out.println(mock);
     Assert.assertThat(mock.toString(), new Equals("vvv"));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值