对于集成测试,我需要在java服务客户端中模拟特定方法,而不破坏其中的其余信息。它没有自构造函数,所以这样的解决方案是不可能的:
private DBClient mockClient = new DBClient(alreadyExistingClient){
@Override
void deleteItem(Item i){
//my stuff goes here
}
};
有没有办法模拟deleteItem方法,以便在现有的DBClient对象中保留凭据,端点等等?
编辑:在这种情况下无法使用mockito
答案 0 :(得分:6)
您可以使用动态代理拦截所需的任何方法调用,因此您可以决定调用实际方法还是执行任何您想要的方法。
这是一个如何拦截方法Set.add()
的示例,您可以对deleteItem()
完全相同
package example.dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Set;
public class SetProxyFactory {
public static Set<?> getSetProxy(final Set<?> s) {
final ClassLoader classLoader = s.getClass().getClassLoader();
final Class<?>[] interfaces = new Class[] {Set.class};
final InvocationHandler invocationHandler = new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
if (method.getName().equals("add")) {
System.out.println("add() intercepted");
// do/return whatever you want
}
// or invoke the real method
return method.invoke(s, args);
}
};
final Object proxy = Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
return (Set<?>) proxy;
}
}
答案 1 :(得分:2)
你可以去低保真并创建一个DBClient类的子类。对于此子类,传递要模拟的DBClient实例。
在子类中使用composition,并将所有方法调用委托给原始DBClient,除了要模拟的所有方法调用。将模拟实现添加到所需的方法中。
这不像模拟框架那样可重用,但应该可行。
DBClient mockDbClient = new DBClient() {
private DBClient dbClientDelegate;
public void setDelegate(DBClient dbClient) {
dbClientDelegate = dbClient;
}
//override all methods.
//delegate to the corresponding method of the dbClientDelegate instance
//overide the method you want to mock, add asserts for method arguments
//return mock data as appropriate
}
mockDbClient.setDelegate(preinstantiatedDbClient);
//inject mockDbClient to test class
//call test class / method
希望这有帮助。
答案 2 :(得分:0)
在Mockito 2+中你可以使用间谍功能:
PrintStream realSystemOut = System.out;
realSystemOut.println("XXX");
PrintStream mockedSystemOut = Mockito.spy(realSystemOut);
Mockito.doNothing().when(mockedSystemOut).println(Mockito.anyString());
mockedSystemOut.println("YYY");
输出:
XXX