Book aBook = mock(Book.class);
当我写入执行
时 aBook.getClass()
它给出
classcom.tw.model.Book$$EnhancerByMockitoWithCGLIB$$feb29207
但我想要: classcom.tw.model.Book
因为Mockito 2.1.0您可以使用getMockCreationSettings()
获取> of what 的详细信息。从文档
添加了通过
访问模拟创建设置的可能性
Mockito.mockingDetails(mock).getMockCreationSettings()
这是一个示例:
@Test
public void aTest() {
Foo mock = Mockito.mock(Foo.class);
MockCreationSettings<?> mockCreationSettings = Mockito.mockingDetails(mock).getMockCreationSettings();
Assert.assertEquals(Foo.class, mockCreationSettings.getTypeToMock());
}
使用instanceof
代替getClass()
void methodUnderTest(Object object) {
if (object instanceof Book) {
Book book = (Book) object;
// read the book
}
}
现在可以轻松地用模拟测试:
@Test
public void bookTest() {
methodUnderTest(mock(Book.class));
}