单元测试框架——Mockito之stubbing语法

书接上回:https://blog.csdn.net/qq_36110736/article/details/107897223

几种Stubbing的方式

1.基础Stubbing

        基础Stubbing主要包括如下几个关键方法:

  • when()
  • thenReturn()
  • thenThrow()
  • doNothing()
  • doThrow()
  • doReturn()

顾名思义其用方法就和名字是一样的

package com.example.demo.stubbing;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class StubbingTest {

    private List list;

    @Before
    public void before(){
        this.list = mock(ArrayList.class);
    }

    @Test
    public void howToUseStubbing(){
        when(list.get(0)).thenReturn("first");
        Assert.assertEquals("first",list.get(0));

        when(list.get(anyInt())).thenThrow(new RuntimeException());
        try {
            list.get(1);
            fail();
        }catch (Exception e){
            assertEquals(e.toString(),new RuntimeException().toString());
        }
    }

    @Test
    public void howToStubbingVoidMethod(){
        doNothing().when(list).clear();
        list.clear();
        verify(list,times(1)).clear();
        doThrow(new RuntimeException()).when(list).clear();
        try {
            list.clear();
            fail();
        }catch (Exception e){
            assertEquals(e.toString(),new RuntimeException().toString());
        }
    }
    @After
    public void destroy(){
        reset(this.list);
    }
}

2.迭代Stubbing

    迭代的实现方式有两种

        第一种就是借助 thenReturn 方法,可以在其中传入多个返回值,也可以多次调用该方法

        第二种就是使用 thenAnswer 方法

    //迭代Stubbing
    @Test
    public void iterateStubbing(){
        when(list.size()).thenReturn(1,2,3,4);
       // when(list.size()).thenReturn(1).thenReturn(2).thenReturn(3).thenReturn(4);
        System.out.println(list.size());
        System.out.println(list.size());
        System.out.println(list.size());
        System.out.println(list.size());
        System.out.println(list.size());
    }

    @Test
    public void stubbingWithAnswer(){
        when(list.get(anyInt())).thenAnswer(invocationOnMock ->{
            Integer index = invocationOnMock.getArgumentAt(0,Integer.class);
            return String.valueOf(index*10);
        } );
        Assert.assertEquals("0",list.get(0));
        Assert.assertEquals("110",list.get(11));
    }

3.真实访问Stubbing

        一般来说mock出来的对象是代理对象,调用的方法也是代理方法,当我们想要调用真实方法时就可以使用 thenCallRealMethod 方法。

被测试类:

package com.example.demo.service;

public class StubbingService {
    public int getInt(){
        System.out.println("===========getInt===========");
        return 10;
    }
    public String getString(){
        System.out.println("======getString======");
       throw new RuntimeException();
    }
}

测试代码:

    //访问真实方法
    @Test
    public void stubbingWithRealCall(){
        StubbingService stubbingService = mock(StubbingService.class);
        // System.out.println("stubbingService的类型时:"+stubbingService.getClass());
        when(stubbingService.getString()).thenReturn("bysen");
        when(stubbingService.getInt()).thenCallRealMethod();
        Assert.assertEquals("bysen",stubbingService.getString());
        Assert.assertEquals(10,stubbingService.getInt());
    }

参考

https://www.bilibili.com/video/BV1jJ411A7Sv?p=4

你可能感兴趣的:(单元测试框架——Mockito之stubbing语法)