zoukankan      html  css  js  c++  java
  • 7. 参数匹配:eq、isA、any

    package lesson7;
    
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.mockito.runners.MockitoJUnitRunner;
    import java.util.ArrayList;
    import java.util.List;
    import static org.mockito.Matchers.eq;
    import static org.mockito.Mockito.*;
    
    @RunWith(MockitoJUnitRunner.class)
    public class ArgumentsMatcherTest {
    
        @Test
        public void basicTest() {
            List<String> list = mock(ArrayList.class);
    //        when(list.get(0)).thenReturn("0");
            /** eq */
            when(list.get(eq(0))).thenReturn("0");
            Assert.assertEquals("0", list.get(0));
            Assert.assertEquals(null, list.get(1));
    
        }
    
        @Test
        public void complexTest() {
            Foo foo = mock(Foo.class);
            /** isA */
            when(foo.func(Mockito.isA(Child1.class))).thenReturn(100);
            int result = foo.func(new Child1());
            Assert.assertEquals(100, result);
            int result2 = foo.func(new Child2());
            Assert.assertEquals(0, result2);
    
            /** 清除foo的mock对象的所有stubbing*/
            reset(foo);
    
            /** any */
            when(foo.func(Mockito.any(Child1.class))).thenReturn(100);
            int result11 = foo.func(new Child2());
            Assert.assertEquals(100, result11);
        }
    
        static class Foo {
            int func(Parent p) {
                return p.work();
            }
        }
    
        interface Parent {
            int work();
        }
    
        class Child1 implements Parent {
            @Override
            public int work() {
                throw new RuntimeException();
            }
        }
    
        class Child2 implements Parent {
            @Override
            public int work() {
                throw new RuntimeException();
            }
        }
    }
  • 相关阅读:
    GitCam一款Gif动画制作软件
    IniParse解析类
    一个网页设计师应该考虑的9件事
    Backit轻松为您的网站创建备份
    [翻译]Web开发牛人访谈:你们都在用什么?
    简单文件服务器
    一个网站访问速度测试工具
    GS运维常用工具及文档
    Oracle快速收集AWR的方案
    Oracle调整内存参后报ORA-00844和ORA-00851
  • 原文地址:https://www.cnblogs.com/yfzhou528/p/14025122.html
Copyright © 2011-2022 走看看