Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get original class name from a mocking object?

Tags:

java

mockito

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

like image 591
Srinivas Avatar asked Oct 16 '25 09:10

Srinivas


1 Answers

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());
}
like image 196
glytching Avatar answered Oct 18 '25 23:10

glytching