5 ms·
OK. first I could be ignorant about Java since I haven't touched it in more than a decade. Which library is doing that? And also what is mock(User.java) returni
by joelbluminator 5y ago
OK. first I could be ignorant about Java since I haven't touched it in more than a decade. Which library is doing that? And also what is mock(User.java) returning - is it an actual User instance or a stub? I want a real User instance (nothing mocked in it) with just the one method mocked.
And again if this is possible I will admit ignorance and tip my hat at the Java guys.
- hota_mazi 5y agoIt's Mockito [1], which has been a standard for a while. There are other libraries and they use different strategies to provide this kind of functionalities (dynamic proxies, bytecode weaving, annotation processing, etc...). [1] https://site.mockito.org/ https://site.mockito.org/
- joelbluminator 5y agoAnd ... is the whole user being mocked or just the method?
- vincnetas 5y agoIt creates a stub, but you can also configure it to pass any method calls to original implementation. You should be tiping your hat i think. https://javadoc.io/static/org.mockito/mockito-core/3.11.2/org/mockito/Answers.html https://javadoc.io/static/org.mockito/mockito-core/3.11.2/or... User mock = mock(User.java) Should have been User mock = mock(User.class)
- hota_mazi 5y agoAh oops, I've been writing exclusively Kotlin for several years, my Java is becoming rusty (no pun intended).
- throwaway_fjmr 5y agoI think what you want is a "spy" (partial mock), not a full "mock", but yes, both are possible. You can partially mock classes, i.e., specific methods only. Syntax is almost the same, instead of mock(User.class) you write spy(User.class).