Java 使用 Mockito 模拟方法的局部变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23236338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:27:32  来源:igfitidea点击:

Using Mockito to mock a local variable of a method

javamockingmockitofunctional-testing

提问by user657592

I have a class Athat needs to the tested. The following is the definition of A:

我有一个A需要测试的课程。以下是 的定义A

public class A {
    public void methodOne(int argument) {
        //some operations
        methodTwo(int argument);
        //some operations
    }

    private void methodTwo(int argument) {
        DateTime dateTime = new DateTime();
        //use dateTime to perform some operations
    }
}

And based on the dateTimevalue some data is to be manipulated, retrieved from the database. For this database, the values are persisted via a JSON file.

并且基于该dateTime值,一些数据将被操作,从数据库中检索。对于此数据库,值通过 JSON 文件持久化。

This complicates things. What I need is to set the dateTimeto some specific date while it is being tested. Is there a way I can mock a local variable's value using mockito?

这使事情复杂化。我需要的dateTime是在测试时将其设置为某个特定日期。有没有办法可以使用 mockito 模拟局部变量的值?

回答by Mureinik

You cannot mock a local variable. What you could do, however, is extract its creation to a protectedmethod and spyit:

您不能模拟局部变量。但是,您可以做的是将其创建提取到一个protected方法中,spy然后:

public class A {
  public void methodOne(int argument) {
    //some operations
    methodTwo(int argument);
    //some operations
  }

  private void methodTwo(int argument) {
    DateTime dateTime = createDateTime();
    //use dateTime to perform some operations
  }

  protected DateTime createDateTime() {
    return new DateTime();
  }
}

public class ATest {
  @Test
  public void testMethodOne() {
    DateTime dt = new DateTime (/* some known parameters... */);
    A a = Mockito.spy(new A());
    doReturn(dt).when(a).createDateTime();
    int arg = 0; // Or some meaningful value...
    a.methodOne(arg);
    // assert the result
}

回答by JB Nizet

The best way to deal with such a problem is to use an injected Clockservice, used to get new instances of DateTime. That way, your test can inject a mock Clock, which returns a specific DateTime instead of the current time.

处理此类问题的最佳方法是使用注入Clock服务,用于获取 DateTime 的新实例。这样,您的测试可以注入一个模拟时钟,它返回一个特定的 DateTime 而不是当前时间。

Note that the new Java 8 time API defines such a Clock class, specifically for that purpose.

请注意,新的 Java 8 时间 API 定义了这样一个Clock 类,专门用于该目的。

回答by Matt

Sorry for the late reply.

这么晚才回复很抱歉。

This might be too much of a hassle, but if you mock the object that can give you the local variable, you can return a mock of it. I'd rather not restructure the code to make testing easier, but its something to consider.

这可能太麻烦了,但是如果您模拟可以为您提供局部变量的对象,则可以返回它的模拟。我宁愿不重组代码以使测试更容易,但它需要考虑。

public class A {

    DateTimeFactory factory;

    private void method() {
        DateTime dateTime = factory.getDateTime();
        //use dateTime to perform some operations
    }
}

In your test you can do something like: when(factoryMock.getDateTime()).doReturn(dateTimeMock)

在您的测试中,您可以执行以下操作: when(factoryMock.getDateTime()).doReturn(dateTimeMock)

The factory mock would need to be injected into the class somehow.

工厂模拟需要以某种方式注入到类中。