Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package org.kuali.student.common.test.mock;

Author(s):
Daniel Epstein
public class MockProxy implements InvocationHandler {
    private Map<String,ObjectmethodReturnMap;
    
    
Creates a new instance of an object that implements the interface you pass it. The Map contains a map of method names to return values. If a method is not in the map, then invoking that method will return null Example: Map<String,Object> methodReturnMap = new HashMap<String,Object>(); MyInterface myObject = MockProxy.newInstance(map, MyInterface.class); map.put("foo", "NEW RETRUN VALUE"); System.out.println(myObject.foo()); // Outputs "NEW RETRUN VALUE"

Parameters:
<T>
methodReturnMap
interfaceClass
Returns:
Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException
    @SuppressWarnings("unchecked")
	public static <T> T newInstance(Map<String,ObjectmethodReturnMapClass<T> interfaceClassthrows InstantiationExceptionIllegalAccessException {
    	return (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[] { interfaceClass }, new MockProxy(methodReturnMap));
    }
    private MockProxy(Map<String,ObjectmethodReturnMap) {
    	super();
    	this. = methodReturnMap;
    	//Create the map if it does not exist
    	if(this.==null){
    		this. = new HashMap<StringObject>();
    	}
    }
	public MockProxy() {
		super();
	}
	/* (non-Javadoc)
	 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
	 */
	public Object invoke(Object proxyMethod methodObject[] args)
			throws Throwable {
		Object result = .get(method.getName());
		if (result instanceof MockArgumentMapper){
		    result = ((MockArgumentMapper)result).getReturnValue(args);
		}
		return result;
	}
New to GrepCode? Check out our FAQ X