Mockito 使用 doReturn 设置方法的返回值


#Java Mockito 测试框架#


doReturn 的作用和 Mockito 使用 thenReturn 设置方法的返回值 相同,但使用方式不同:

import org.junit.Assert;
import org.junit.Test;
import org.mockito.MockitoAnnotations;

import java.util.Random;

import static org.mockito.Mockito.*;

public class MockitoDemo {

    @Test
    public void test() {

        MockitoAnnotations.initMocks(this);

        Random random = mock(Random.class);
        doReturn(1).when(random).nextInt();

        Assert.assertEquals(1, random.nextInt());

    }

}

( 本文完 )