diff options
author | Hugo Hudson <hugohudson@google.com> | 2012-01-11 11:55:10 +0000 |
---|---|---|
committer | Hugo Hudson <hugohudson@google.com> | 2012-01-11 11:55:10 +0000 |
commit | ae4e4ac4facc3eacff13ab7ed115292fb5009d1e (patch) | |
tree | 913016136de23bf92394bba753f8b11714f7e7d7 | |
parent | ebb9a5e46fa2c58fa091a8de2c12d87828fae2b0 (diff) | |
download | littlemock-ae4e4ac4facc3eacff13ab7ed115292fb5009d1e.tar.gz |
Update to r7 of LittleMock.
- Fix to mocking concrete classes - get invocation handler.
Change-Id: I8b000586cd502322a1db3828d59c66c07fa52958
-rw-r--r-- | src/com/google/testing/littlemock/LittleMock.java | 34 | ||||
-rw-r--r-- | tests/com/google/testing/littlemock/LittleMockTest.java | 5 |
2 files changed, 25 insertions, 14 deletions
diff --git a/src/com/google/testing/littlemock/LittleMock.java b/src/com/google/testing/littlemock/LittleMock.java index c8f2b2f..efd205c 100644 --- a/src/com/google/testing/littlemock/LittleMock.java +++ b/src/com/google/testing/littlemock/LittleMock.java @@ -1011,18 +1011,6 @@ public class LittleMock { return DEFAULT_RETURN_VALUE_LOOKUP.get(returnType); } - /** - * If the input object is one of our mocks, returns the {@link DefaultInvocationHandler} - * we constructed it with. Otherwise fails with {@link IllegalArgumentException}. - */ - private static DefaultInvocationHandler getHandlerFrom(Object mock) { - InvocationHandler invocationHandler = Proxy.getInvocationHandler(mock); - if (!(invocationHandler instanceof DefaultInvocationHandler)) { - throw new IllegalArgumentException("not a valid mock: " + mock); - } - return (DefaultInvocationHandler) invocationHandler; - } - /** Gets a suitable class loader for use with the proxy. */ private static ClassLoader getClassLoader() { return LittleMock.class.getClassLoader(); @@ -1042,6 +1030,28 @@ public class LittleMock { } } + /** + * If the input object is one of our mocks, returns the {@link DefaultInvocationHandler} + * we constructed it with. Otherwise fails with {@link IllegalArgumentException}. + */ + private static DefaultInvocationHandler getHandlerFrom(Object mock) { + try { + InvocationHandler invocationHandler = Proxy.getInvocationHandler(mock); + if (invocationHandler instanceof DefaultInvocationHandler) { + return (DefaultInvocationHandler) invocationHandler; + } + } catch (IllegalArgumentException expectedIfNotAProxy) {} + try { + Class<?> proxyBuilder = Class.forName("com.google.dexmaker.stock.ProxyBuilder"); + Method getHandlerMethod = proxyBuilder.getMethod("getInvocationHandler", Object.class); + Object invocationHandler = getHandlerMethod.invoke(proxyBuilder, mock); + if (invocationHandler instanceof DefaultInvocationHandler) { + return (DefaultInvocationHandler) invocationHandler; + } + } catch (Exception expectedIfNotAProxyBuilderMock) {} + throw new IllegalArgumentException("not a valid mock: " + mock); + } + /** Create a dynamic proxy for the given class, delegating to the given invocation handler. */ private static Object createProxy(Class<?> clazz, InvocationHandler handler) { if (clazz.isInterface()) { diff --git a/tests/com/google/testing/littlemock/LittleMockTest.java b/tests/com/google/testing/littlemock/LittleMockTest.java index e8c7b1e..fde8c7c 100644 --- a/tests/com/google/testing/littlemock/LittleMockTest.java +++ b/tests/com/google/testing/littlemock/LittleMockTest.java @@ -1413,13 +1413,14 @@ public class LittleMockTest extends TestCase { } catch (IllegalStateException expected) {} } - public class Jim { + public static class Jim { public void bob() { fail(); } } - public void testMockingConcreteClasses() throws Exception { + // Does not work on JVM, android only. + public void suppress_testMockingConcreteClasses() throws Exception { Jim mock = mock(Jim.class); mock.bob(); } |