Mockito 将存根计算为调用
Mockito counting stubbing as invocation
正在尝试使用自定义匹配器将 class 与 2 个可能的 invocation/return 路径存根...运行 成为一个兴趣问题。
这是我写的一个测试来说明...
这可能很难实现,但我希望在存根第二个 when(...).thenReturn(...)
时不会调用第一个 ArgumentMatcher
但是 运行 下面的代码在标准输出上打印 foobar
。我们可以做些什么来防止这种行为?或者我是否通过尝试使用多个自定义 ArgumentMatcher
存根单个模拟来使用错误的模式
仅供参考 - powermock
在我的 class 其他测试路径上(不确定这是否重要,但我确实在堆栈跟踪中看到了它)
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import java.io.File;
import java.io.FilenameFilter;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MyTest {
@Test
public void name() throws Exception {
File file = mock(File.class);
when(file.list(argThat(new ArgumentMatcher<FilenameFilter>() {
@Override
public boolean matches(Object argument) {
System.out.println("foobar");
return 1 + 1 >2;
}
}))).thenReturn(null);
// at this point, mockito will attempt to run the previous matcher, treating this stub code as invocation ... and printing out 'foobar'
when(file.list(argThat(new ArgumentMatcher<FilenameFilter>() {
@Override
public boolean matches(Object argument) {
System.out.println("barbar");
return true;
}
}))).thenReturn(null);
}
}
编辑 添加评论以帮助说明
如果您使用 doReturn()
语法,则不会调用该方法。
doReturn(null).when(file).list(argThat(new ArgumentMatcher<FilenameFilter>() {
@Override
public boolean matches(Object argument) {
System.out.println("barbar");
return true;
}
}));
参见this answer for more details. Also, the docs解释这个用例(强调我的):
You can use doReturn(), [...] in place of the corresponding call with when(), for any method. It is necessary when you:
- stub void methods
- stub methods on spy objects (see below)
- stub the same method more than once, to change the behaviour of a mock in the middle of a test.
正在尝试使用自定义匹配器将 class 与 2 个可能的 invocation/return 路径存根...运行 成为一个兴趣问题。
这是我写的一个测试来说明...
这可能很难实现,但我希望在存根第二个 when(...).thenReturn(...)
ArgumentMatcher
但是 运行 下面的代码在标准输出上打印 foobar
。我们可以做些什么来防止这种行为?或者我是否通过尝试使用多个自定义 ArgumentMatcher
仅供参考 - powermock
在我的 class 其他测试路径上(不确定这是否重要,但我确实在堆栈跟踪中看到了它)
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import java.io.File;
import java.io.FilenameFilter;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MyTest {
@Test
public void name() throws Exception {
File file = mock(File.class);
when(file.list(argThat(new ArgumentMatcher<FilenameFilter>() {
@Override
public boolean matches(Object argument) {
System.out.println("foobar");
return 1 + 1 >2;
}
}))).thenReturn(null);
// at this point, mockito will attempt to run the previous matcher, treating this stub code as invocation ... and printing out 'foobar'
when(file.list(argThat(new ArgumentMatcher<FilenameFilter>() {
@Override
public boolean matches(Object argument) {
System.out.println("barbar");
return true;
}
}))).thenReturn(null);
}
}
编辑 添加评论以帮助说明
如果您使用 doReturn()
语法,则不会调用该方法。
doReturn(null).when(file).list(argThat(new ArgumentMatcher<FilenameFilter>() {
@Override
public boolean matches(Object argument) {
System.out.println("barbar");
return true;
}
}));
参见this answer for more details. Also, the docs解释这个用例(强调我的):
You can use doReturn(), [...] in place of the corresponding call with when(), for any method. It is necessary when you:
- stub void methods
- stub methods on spy objects (see below)
- stub the same method more than once, to change the behaviour of a mock in the middle of a test.