zoukankan      html  css  js  c++  java
  • 4-5. when-thenXX、doXX-when、Answer、thenCallRealMethod

    package lesson4_5;
    
    import org.junit.After;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.invocation.InvocationOnMock;
    import org.mockito.runners.MockitoJUnitRunner;
    import org.mockito.stubbing.Answer;
    import java.util.ArrayList;
    import java.util.List;
    import static org.junit.Assert.assertEquals;
    import static org.mockito.Mockito.*;
    import static org.mockito.Mockito.when;
    
    @RunWith(MockitoJUnitRunner.class)
    public class _0_StubbingTest {
        public List<String> list;
    
        @Before
        public void init() {
            list = mock(ArrayList.class);
        }
    
        @Test
        public void howToUseStubbing() {
            when(list.get(0)).thenReturn("11");
            assertEquals("11", list.get(0));
            when(list.get(anyInt())).thenThrow(RuntimeException.class);
            try {
                list.get(0);
                Assert.fail();
            } catch (Exception e) {
                assertEquals(RuntimeException.class, e.getClass());
            }
        }
    
        /**
         * void method
         * verify
         */
        @Test
        public void howToStubbingVoidMethod() {
            doNothing().when(list).clear();
            list.clear();
            verify(list, times(1)).clear();
    
            doThrow(RuntimeException.class).when(list).clear();
            try {
                list.clear();
                Assert.fail();
            } catch (Exception e) {
                assertEquals(RuntimeException.class, e.getClass());
            }
        }
    
        @Test
        public void stubbingDoReturn() {
            /**
             * when-thenReturn <=> doReturn-when
             */
            when(list.get(0)).thenReturn("11");
            doReturn("22").when(list).get(1);
            assertEquals("11", list.get(0));
            assertEquals("22", list.get(1));
        }
    
        @Test
        public void iterateSubbing() {
            when(list.size()).thenReturn(1, 2, 3);
            assertEquals(1, list.size());
            assertEquals(2, list.size());
            assertEquals(3, list.size());
            assertEquals(3, list.size());
        }
    
        /**
         * Answer
         */
        @Test
        public void stubbingWithAnswer() {
            when(list.get(anyInt())).thenAnswer(new Answer<String>() {
                @Override
                public String answer(InvocationOnMock invocation) throws Throwable {
                    Integer index = invocation.getArgumentAt(0, Integer.class);
                    /**返回值是下标值的10倍**/
                    return String.valueOf(index * 10);
                }
            });
            assertEquals("0", list.get(0));
            assertEquals("10", list.get(1));
            assertEquals("20", list.get(2));
            assertEquals("30", list.get(3));
            assertEquals("9990", list.get(999));
        }
    
        /**
         * when-thenCallRealMethod()
         */
        @Test
        public void stubbingWithRealCall() {
            StubbingService service = mock(StubbingService.class);
            System.out.println(service.getClass());
            System.out.println(service.getFoo());
            System.out.println(service.getFooException());
            /**调用真正方法**/
            when(service.getFoo()).thenCallRealMethod();
            assertEquals("foo", service.getFoo());
            /**调用真正方法**/
            when(service.getFooException()).thenCallRealMethod();
            try {
                service.getFooException();
            } catch (Exception e) {
                assertEquals(RuntimeException.class, e.getClass());
            }
        }
    
        @After
        public void destroy() {
            reset(list);
        }
    }
    package lesson4_5;
    
    public class StubbingService {
        public String getFoo() {
            return "foo";
        }
    
        public int getFooException() {
            throw new RuntimeException();
        }
    }
  • 相关阅读:
    网易2019实习生招聘编程第3题——牛牛找工作
    Linux find、locate、whereis、which命令
    Linux 常用命令
    Java线程池
    java连接池的maxIdle该如何配置
    Idea和redis的坑
    微服务架构下分布式事务解决方案——阿里GTS
    spring管理bean的三种创建方式
    jvm内存过高及那些对象导致内存过高,那些对象不会被gc回收
    Java虚拟机 运行时数据区
  • 原文地址:https://www.cnblogs.com/yfzhou528/p/14025030.html
Copyright © 2011-2022 走看看