目录

(六) mockito 常用方法

doReturn()、doThrow()、doAnswer()、doNothing()、doCallRealMethod()系列方法的运用

通过when(Object)为无返回值的函数打桩有不同的方法,因为编译器不喜欢void函数在括号内…

使用doThrow(Throwable) 替换stubVoid(Object)来为void函数打桩是为了与doAnswer()等函数族保持一致性。

当你想为void函数打桩时使用含有一个exception 参数的doAnswer() :

import org.junit.Test;
import java.util.LinkedList;
import static org.mockito.Mockito.*;


public class MockitoDemo {

    @Test(expected = RuntimeException.class)
    public void when_doThrow() throws RuntimeException {

        LinkedList mockedList = mock(LinkedList.class);
        doThrow(new RuntimeException()).when(mockedList).clear();

        //following throws RuntimeException:
        // 下面的代码会抛出异常
        mockedList.clear();
    }
}

当你调用doThrow(), doAnswer(), doNothing(), doReturn()doCallRealMethod() 这些函数时可以在适当的位置调用when()函数,例如下面这些功能时这是必须的:

  • 测试void函数
  • 在受监控的对象上测试函数
  • 不知一次的测试为同一个函数,在测试过程中改变mock对象的行为。

但是在调用when()函数时你可以选择是否调用这些上述这些函数。

原始封面

https://images.unsplash.com/photo-1604328698692-f76ea9498e76?w=300