Java 使用Mockito模拟方法的局部变量
我有一个需要测试的类Java 使用Mockito模拟方法的局部变量,java,mocking,mockito,functional-testing,Java,Mocking,Mockito,Functional Testing,我有一个需要测试的类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 Dat
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
}
}
根据dateTime
值,将对一些数据进行操作,并从数据库中检索。对于该数据库,值通过JSON文件持久化
这使事情复杂化。我需要的是在测试时将
dateTime
设置为某个特定的日期。有没有办法用mockito模拟局部变量的值?你不能模拟局部变量。但是,您可以将其创建内容提取到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
}
处理此类问题的最佳方法是使用注入的
Clock
服务,用于获取DateTime的新实例。这样,您的测试就可以注入一个模拟时钟,它返回一个特定的日期时间而不是当前时间
请注意,新的Java 8时间API专门为此定义了这样一个对象。这可能会太麻烦,但如果您模拟可以提供局部变量的对象,则可以返回该对象的模拟。我宁愿不重新构造代码,以使测试更容易,但它需要考虑。
public class A {
DateTimeFactory factory;
private void method() {
DateTime dateTime = factory.getDateTime();
//use dateTime to perform some operations
}
}
在测试中,您可以执行以下操作:when(factoryMock.getDateTime()).doReturn(dateTimeMock)
工厂模拟需要以某种方式注入到类中。我在这个主题上发现的其他问题可能都没有额外的抽象层。此外,它们中的大多数都是通过模拟类的某个方法实例化局部变量的。没有其他方法吗?实际上你必须使用Mockito.doReturn(dt).when(a).createDateTime()请看这当然是正确的@Alex,谢谢你的注意!已编辑并修复。如果类没有默认构造函数,则无法
spy()。想要避免它。@WesternGun在上面的代码段中,您提供了一个要监视的A
的实例。它与默认构造函数无关-您可以根据自己的意愿构造此实例。这需要jdk1.8,对吗?我目前使用的是1.7,不打算很快升级。不。您只需要定义自己的时钟类或接口,并调用Clock.newDateTime()
而不是newDateTime()
来获取当前时间。