Mockito varargs 参数匹配器的无效使用

Fee*_*eco 2 java mockito

测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MockabstractionApplication.class)
public class SimpleTest {

    @SpyBean
    private SimpleService spySimpleService;

    @Before
    public void setup() {
        initMocks(this);
    }

    @Test //fails
    public void test() throws Exception {
        when(spySimpleService.test(1, Mockito.<String>anyVararg())).thenReturn("Mocked!");
    }

}
Run Code Online (Sandbox Code Playgroud)

服务

@Service
public class SimpleService {

    public String test(int i, String... args) {
        return "test";
    }

}
Run Code Online (Sandbox Code Playgroud)

测试失败并显示下一条消息:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:参数匹配器的使用无效!预计 2 个匹配者,1 个记录:

我必须使用 int 作为第一个参数和任意数量的可变参数。

Mat*_*nkt 5

如果您对一个参数使用匹配器,则必须对所有参数使用它。

when(spySimpleService.test(Mockito.eq(1), Mockito.<String>anyVararg())).thenReturn("Mocked!");
Run Code Online (Sandbox Code Playgroud)