Book aBook = mock(Book.class);
when I write to execute
aBook.getClass()
it gives
classcom.tw.model.Book$$EnhancerByMockitoWithCGLIB$$feb29207
But I want : classcom.tw.model.Book
Book aBook = mock(Book.class);
when I write to execute
aBook.getClass()
it gives
classcom.tw.model.Book$$EnhancerByMockitoWithCGLIB$$feb29207
But I want : classcom.tw.model.Book
Since Mockito 2.1.0 you can use getMockCreationSettings()
to get details on what was mocked. from the docs
Added the possibility to access the mock creation settings via
Mockito.mockingDetails(mock).getMockCreationSettings()
Here's an example:
@Test
public void aTest() {
Foo mock = Mockito.mock(Foo.class);
MockCreationSettings<?> mockCreationSettings = Mockito.mockingDetails(mock).getMockCreationSettings();
Assert.assertEquals(Foo.class, mockCreationSettings.getTypeToMock());
}
Use instanceof
instead of getClass()
void methodUnderTest(Object object) {
if (object instanceof Book) {
Book book = (Book) object;
// read the book
}
}
this can now easily be tested with a mock:
@Test
public void bookTest() {
methodUnderTest(mock(Book.class));
}