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();
    }
}
原文地址:https://www.cnblogs.com/yfzhou528/p/14025030.html