Mockito uses reflection and CGLib to extend the Employee class to a dynamically created superclass. As part of this, it starts with all Employee public constructors - including the default constructor, which is still around, but private if you declared a constructor that accepts parameters.
public <T> T imposterise(final MethodInterceptor interceptor, Class<T> mockedType, Class<?>... ancillaryTypes) { try { setConstructorsAccessible(mockedType, true); Class<?> proxyClass = createProxyClass(mockedType, ancillaryTypes); return mockedType.cast(createProxy(proxyClass, interceptor)); } finally { setConstructorsAccessible(mockedType, false); } } private void setConstructorsAccessible(Class<?> mockedType, boolean accessible) { for (Constructor<?> constructor : mockedType.getDeclaredConstructors()) { constructor.setAccessible(accessible); } }
I assume that it calls the default constructor when creating the superclass, although I have not tested it. You can test it yourself by declaring the private constructor Employee () and putting several protocols into it.
Lunivore
source share