Created
August 18, 2014 22:01
-
-
Save jimnanney/b0fc0fb62e1d126b325d to your computer and use it in GitHub Desktop.
Beginning of a mock framework for java 1.4.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package unittest.fixture; | |
public interface AnyMock { | |
public String getName(); | |
public void setName(String value); | |
public String printName(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package unittest.fixture; | |
import java.lang.reflect.*; | |
import java.util.HashMap; | |
public class Factory implements InvocationHandler { | |
protected Class[] interfaces; | |
protected Object returnValue; | |
protected HashMap returnValues; | |
protected Factory(Class[] interfaces) { | |
this.returnValues = new HashMap(); | |
this.interfaces = interfaces; | |
} | |
public static Factory MockFor(Class[] interfaces) { | |
return new Factory(interfaces); | |
} | |
public Factory returns(Object value) { | |
this.returnValue = value; | |
return this; | |
} | |
public Factory forMethod(String methodName) { | |
this.returnValues.put(methodName, returnValue); | |
returnValue = null; | |
return this; | |
} | |
public Object Build() { | |
return Proxy.newProxyInstance(this.getClass().getClassLoader(), interfaces, this); | |
} | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | |
String methodName = method.getName(); | |
if (returnValues.containsKey(methodName)) return returnValues.get(methodName); | |
if (methodName.startsWith("get")) return returnValues.get(propertyName(methodName, "get")); | |
if (methodName.startsWith("set")) { | |
returnValues.put(propertyName(methodName, "set"), args[0]); | |
return null; | |
} | |
return null; | |
} | |
private String propertyName(String methodName, String stripWord) { | |
return methodName.substring(methodName.indexOf(stripWord) + stripWord.length()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package unittest.fixture; | |
import junit.framework.TestCase; | |
import junit.framework.Assert; | |
import unittest.fixture.AnyMock; | |
/* | |
* | |
public interface AnyMock { | |
public String getName(); | |
public void setName(String value); | |
public String printName(); | |
} | |
*/ | |
public class FactoryTest extends TestCase { | |
protected void setUp() throws Exception { | |
super.setUp(); | |
} | |
protected void tearDown() throws Exception { | |
super.tearDown(); | |
} | |
public void test_stores_returnValues() { | |
AnyMock mock = (AnyMock) Factory.MockFor(new Class[] { AnyMock.class }) | |
.returns("John") | |
.forMethod("printName") | |
.Build(); | |
Assert.assertEquals("John", mock.printName()); | |
} | |
public void test_returns_null_for_unimplemented_methods() { | |
AnyMock mock = (AnyMock) Factory.MockFor(new Class[] { AnyMock.class }).Build(); | |
Assert.assertNull(mock.printName()); | |
} | |
public void test_automatic_property_getter_setter_when_none_supplied() { | |
AnyMock mock = (AnyMock) Factory.MockFor(new Class[] { AnyMock.class }).Build(); | |
mock.setName("John"); | |
Assert.assertEquals("John", mock.getName()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment