gpt4 book ai didi

java - Mockito doReturn().when() 调用原始方法

转载 作者:行者123 更新时间:2023-11-28 21:07:33 27 4
gpt4 key购买 nike

我无法让 Mockito 覆盖我正在测试的类中的方法。

@Test
public void test_classToTest() throws Exception {
DependencyA dependencyA = mock(DependencyA.class);
DependencyB dependencyB = mock(DependencyB.class);
DependencyC dependencyC = mock(DependencyC.class);

ClassToTest classToTest = ClassToTest.builder().dependencyA(dependencyA)
.dependencyB(dependencyB).dependencyC(dependencyC).build();

classToTest= Mockito.spy(classToTest);

Mockito.doReturn("This is not the method you are looking for").when(classToTest).storeContent(null, null, null);

String result = classToTest.copyContent(someVariable, SOME_CONSTANT);

我试图覆盖的方法是从 classToTest.copyContent() 内部调用的 classToTest.storeContent()。我知道这个类有点臭,但我无法重构它。但是,这不是一个非常复杂的设置,我不确定为什么调用实际的 .storeContent() 方法。

最佳答案

我建议使用 ArgumentMatchers.any 而不是使用 null 参数来设置模拟的 storeContent 方法

例如

import static org.mockito.ArgumentMatchers.*;

// ...

Mockito.doReturn("This is not the method you are looking for").when(classToTest).storeContent(any(), any(), any());

关于java - Mockito doReturn().when() 调用原始方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52336504/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com