Implement very simplified version of Mockito http://site.mockito.org.
Methods to implement:
mock– creates mocked object (proxy)when– returns object with one methodthenReturnwhich has to take the same parameter aswhenmethod call
MyClass m = MyMockito.mock(MyClass.class);
MyMockito.when(m.someMethod()).thenReturn(someValue);
m.someMethod(); // returns `someValue`Use library CGLib http://mvnrepository.com/artifact/cglib/cglib/3.2.6
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(classObject); // parameter of mock method
enhancer.setCallback(new MyMockClassCallback());
return (T) enhancer.create(); // T is template arg of mock methodwhere MyMockClassCallback is a class which looks like this:
private static class MyMockClassCallback implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) {
return methodProxy.invokeSuper(o, args);
}
}It is easy to call different method on o in intercept() which can
cause StackOverflowError.
mvn testor simply run from your IDE :)
Good luck! :)