单元测试时,假如你遇到某个内部方法无法正常调用;我们可以使用mock工具去解决,方法如下:
引入依赖
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
Mock指定方法
@Before
public void install() throws Exception {
// mock bean
User user = mock(User.class);
// mock method without return
doNothing().when(user).login(anyString(), anyString(), anyString());
// mock method with return
when(user).login(anyString(), anyString(), anyString()).thenReturn("OK");
}