如何让 Mockito 模拟另一个文件中的常量?

HLH*_*HLH 5 java testing mockito

编辑:我正在测试的方法调用在另一个类中定义的这个常量,所以我想测试该方法是否独立于其他类定义常量的方式工作。嘲笑它是我能想到的第一件事,但我对如何以干净、安全的方式测试它的其他想法持开放态度

(类、方法和变量名称已泛化)

我正在尝试弄清楚如何编写测试。我的一个方法从另一个类获取常量,如下所示:

OtherClass.CONSTANT
Run Code Online (Sandbox Code Playgroud)

这个常数定义为:

public static final List<Long> CONSTANT =
  ImmutableList.of(1, 2);
Run Code Online (Sandbox Code Playgroud)

在测试这个方法时,我想模拟这个调用。我试过了

when(OtherClass.CONSTANT).thenReturn(ImmutableList.of(1, 2));
Run Code Online (Sandbox Code Playgroud)

但这给了我这个错误:

RegularImmutableList cannot be returned by otherFunction()
otherFunction() should return String
Run Code Online (Sandbox Code Playgroud)

otherFunction() 是代码库中的其他函数,它似乎与我一直在处理的任何内容无关。

我也尝试过

doReturn(ImmutableList.of(1, 2)).when(OtherClass.CONSTANT);
Run Code Online (Sandbox Code Playgroud)

但是,正如您可能猜到的那样,它给了我这个错误:

Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
Run Code Online (Sandbox Code Playgroud)

我非常不知道应该如何嘲笑这个常量。

spr*_*ter 6

正如您所发现的,您无法模拟常量的值。

最简单的方法可能是将您的设计转换为使用接口来提供值,而不是直接使用该值。

就像是:

interface ConstantSupplier {
    List<Long> get();
}

public MyClass(ConstantSupplier supplier) {
    this.supplier = supplier;
}
Run Code Online (Sandbox Code Playgroud)

然后您将对常量的引用替换为supplier.get()

现在很容易模拟:

ConstantSupplier supplier = mock(ConstantSupplier.class);
when(supplier.get()).thenReturn(List.of(4L, 9L));
Run Code Online (Sandbox Code Playgroud)

您的非模拟代码可以使用 lambda 来提供实际值:

obj = new MyClass(() -> OtherClass.CONSTANT);
Run Code Online (Sandbox Code Playgroud)