要直接回答你的问题,是的,你可以模拟某些方法而无需模拟其他方法。这称为部分模拟。有关更多信息,请参见Mockito文档。
例如,你可以在测试中执行以下操作:
Stock stock = mock(Stock.class);when(stock.getPrice()).thenReturn(100.00); // Mock implementationwhen(stock.getQuantity()).thenReturn(200); // Mock implementationwhen(stock.getValue()).thenCallRealMethod(); // Real implementation
在这种情况下,除非thenCallRealMethod()在when(..)子句中指定,否则将模拟每个方法的实现。
还有一种可能是使用间谍而不是模拟来代替间谍:
Stock stock = spy(Stock.class);when(stock.getPrice()).thenReturn(100.00); // Mock implementationwhen(stock.getQuantity()).thenReturn(200); // Mock implementation// All other method call will use the real implementations
在这种情况下,所有方法实现都是真实的,除非你使用定义了模拟行为when(..)。
when(Object)与上一个示例一样,当与间谍一起使用时,有一个重要的陷阱。将调用real方法(因为在运行时stock.getPrice()之前已对其进行了评估when(..))。如果你的方法包含不应调用的逻辑,则可能会出现问题。你可以这样编写前面的示例:
Stock stock = spy(Stock.class);doReturn(100.00).when(stock).getPrice(); // Mock implementationdoReturn(200).when(stock).getQuantity(); // Mock implementation// All other method call will use the real implementations
另一种可能是使用org.mockito.Mockito.CALLS_REAL_METHODS,例如:
Stock MOCK_STOCK = Mockito.mock( Stock.class, CALLS_REAL_METHODS );
这将未打桩的调用委托给实际的实现。
但是,对于你的示例,我相信它仍然会失败,因为你所嘲笑的实现getValue()依赖于quantity和
price而不是
getQuantity()and getPrice()。
另一种可能性是完全避免模拟:
@Testpublic void getValueTest() { Stock stock = new Stock(100.00, 200); double value = stock.getValue(); assertEquals("Stock value not correct", 100.00*200, value, .00001);}


