인터페이스의 경우 java.lang.reflect.Proxy의 기능을 사용할 수 있습니다. 당신이 MessageConstants 클래스에 대한 답변을 스텁하고자한다고 가정하면, 코드는 유사합니다 :
InvocationHandler handler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (String.class.equals(method.getReturnType()))
return "proxy generated method return";
throw new AbstractMethodError("Not implemented");
}
};
Class<?> proxyClass = Proxy.getProxyClass(MessageConstants.class.getClassLoader(), new Class[] { MessageConstants.class });
MessageConstants messageConstants = (MessageConstants) proxyClass.getConstructor(new Class[] {InvocationHandler.class}).newInstance(new Object[] { handler });
System.out.println(messageConstants.description());
messageConstants.getBoolean("someBoolean");
나머지는 단지 배관 동안,
proxy generated method return
Exception in thread "main" java.lang.Error: Not implemented
at xxx.Application$1.invoke(Application.java:48)
at $Proxy0.getBoolean(Unknown Source)
at xxx.Application.main(Application.java:64)
의 InvocationHandler는 스터 빙 드라이브가 발생합니다.
허락 해 주셔서 감사합니다 (m); 이건 내가 찾던 .. 툴링의 선택과 관련하여 스텁 클래스의 문제점은 실제 인터페이스에서 뭔가를 할 때마다 새로 고침해야한다는 것입니다. 그래서 자동으로 스텁을 만드는 무언가가 필요합니다. 살인자는 좋다, 나는 이것을 시도 할 것이다. –