【问题标题】:Is it possible to mock a single method in an already existing object?是否可以在已经存在的对象中模拟单个方法?
【发布时间】:2013-04-19 17:19:11
【问题描述】:

对于集成测试,我需要在 java 服务客户端中模拟特定方法,而不会破坏其中的其余信息。它没有自构造函数,所以这样的解决方案是不可能的:

private DBClient mockClient = new DBClient(alreadyExistingClient){
    @Override
    void deleteItem(Item i){
        //my stuff goes here
    }
};

有没有办法模拟 deleteItem 方法,以便将凭据、端点等保存在现有的 DBClient 对象中?

编辑:在这种情况下,mockito 不可用

【问题讨论】:

标签: java easymock powermock white-box


【解决方案1】:

您可以使用动态代理来拦截您想要的任何方法调用,因此您可以决定是调用真实方法还是执行您想要的任何操作。

这是一个如何拦截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;
    }
}

【讨论】:

  • 我什至不知道这是可能的,它绝对适用于这里的情况。感谢您发布此内容,即使我最终没有使用它,我也从中学到了很多东西。
  • 一个有趣的解决方案。您仍然可以在实例化代理之前检查危险方法是否存在。以确保它不会被无意中重命名。
【解决方案2】:

您可以低保真并创建 DBClient 类的子类。向该子类传递您要模拟的 DBClient 实例。

在子类中使用组合,并将所有方法调用委托给原始 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

希望这会有所帮助。

【讨论】:

  • 我试图避免这种情况,但看起来这可能是我在这种情况下的唯一选择。谢天谢地,这是一个非常孤立的情况,我再也不需要做这样的事情了。
【解决方案3】:

在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-14
    • 2017-02-24
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2011-09-11
    • 1970-01-01
    相关资源
    最近更新 更多