Mockito.doReturn
时间: 2024-05-05 21:15:34 浏览: 10
Mockito.doReturn() is a method in the Mockito library that allows you to specify a return value for a method call on a mock object. It is used to stub a method call on a mock object to return a specific value, rather than executing the actual method code.
Here is an example of how to use Mockito.doReturn():
```
// Create a mock object of a class
SomeClass myMock = Mockito.mock(SomeClass.class);
// Stub the method call to return a specific value
Mockito.doReturn("hello").when(myMock).someMethod();
// Call the method on the mock object
String result = myMock.someMethod();
// Verify that the method was called and returned the correct value
assertEquals("hello", result);
```
In this example, we create a mock object of the SomeClass class and then use Mockito.doReturn() to stub the someMethod() method to return the string "hello". When we call someMethod() on the mock object, it will return "hello" instead of executing the actual method code. We then verify that the method was called and returned the correct value using the assertEquals() method.