public class TestProxy extends Object
Utility class for simple mocking of interfaces.
The idea is, to isolate a class under test from its dependencies, by using mocks or dummies as dependencies. As some interfaces are rather large and contain many methods, implementing them for each test results in lots of boilerplate code which is also hard to maintain.
This class provides an alternative way to easily create proxys with the
proxy
-methods. You can either use the more explicit way with
proxy(ClassLoader, List, List, List)
or the more compact
proxy(Object..)
A new proxy class will be created
(using the optionally given ClassLoader) which implements all the given
interfaces. Additionally, you can provide one ore more delegate objects.
Whenever a method is invoked on the proxy, the given objects will be checked
if they contain a method with a matching signature, and if so, that method
will be invoked an behalf of the proxy. If there are no object(s) or no
matching method was found, an UnsupportedOperationException
with a
meaningful message will be thrown.
Modifier and Type | Class and Description |
---|---|
static class |
TestProxy.IgnoreMethod |
static interface |
TestProxy.Option |
Constructor and Description |
---|
TestProxy() |
Modifier and Type | Method and Description |
---|---|
static Optional<Tuple2<Object,Method>> |
findHandler(List<Object> handlers,
Method method) |
static TestProxy.IgnoreMethod |
ignoreMethod(String name) |
static <T> T |
proxy(ClassLoader classLoader,
List<Class<?>> interfaces,
List<Object> delegates,
List<TestProxy.Option> options)
Creates a proxy object.
|
static <T> T |
proxy(Object... classLoaderOrInterfaceOrDelegateOrOption)
Compact version of
proxy(ClassLoader, List, List, List) . |
public static TestProxy.IgnoreMethod ignoreMethod(String name)
public static Optional<Tuple2<Object,Method>> findHandler(List<Object> handlers, Method method)
public static <T> T proxy(ClassLoader classLoader, List<Class<?>> interfaces, List<Object> delegates, List<TestProxy.Option> options)
Creates a proxy object.
classLoader
- The classloader to load the interfaces.interfaces
- Die Interfaces, die das Proxy-Objekt implementieren soll.delegates
- The objects to which method-invocations of the proxy will be
delegated to.UnsupportedOperationException
- If no delegate method was found.public static <T> T proxy(Object... classLoaderOrInterfaceOrDelegateOrOption)
Compact version of proxy(ClassLoader, List, List, List)
.
classLoaderOrInterfaceOrDelegateOrOption
- Variable set of parameters used the following way: 1) if instance
of TestProxy.Option
, than used as option, 2) if instance of
ClassLoader, then used to create the proxy instance, 3) if a class
(no interface), used as delegate object, 4) else it will be used
as interface to be implemented by the proxy.proxy(ClassLoader, List, List, List)
Copyright © 2019. All rights reserved.