doReturn 的作用和 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()); } }
转载:https://www.letianbiji.com/java-mockito/mockito-doreturn.html