在使用Mockito时,如果要模拟一个无返回值的方法,可以使用doNothing()
方法。例如:
// 创建一个mock对象
SomeClass someClassMock = Mockito.mock(SomeClass.class);
// 模拟无返回值的方法
Mockito.doNothing().when(someClassMock).voidMethod();
这样就可以模拟SomeClass
类中的voidMethod()
方法,使其在被调用时不做任何操作。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:java newinstance方法的返回值是什么