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
Using Mockito to mock a local variable of a method
提问by user657592
I have a class A
that 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 dateTime
value 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 dateTime
to 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 protected
method and spy
it:
您不能模拟局部变量。但是,您可以做的是将其创建提取到一个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 Clock
service, 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.
工厂模拟需要以某种方式注入到类中。