简体   繁体   English

参数匹配器的无效使用。 如果此方法使用另一种私有方法,我该如何测试它?

[英]Invalid use of argument matchers. How can I test it, if this method use another private method?

I have method:我有方法:

public void loadPlatformDependencies() {
    try {
        dependenciesRepository.deleteAll();
        dependenciesRepository.saveAll(pullLastDependencies());
        publisher.publishEvent(new LoadedDependenciesEvent());
    } catch (Exception e) {
        LOGGER.error("Failed to load dependencies", e);
    }
}

And I try to test it:我尝试测试它:

   @Test
    public void testLoadPlatformDependencies() {
        ArgumentCaptor<Iterable<Dependency>> captor = ArgumentCaptor.forClass(Iterable.class);
        when(dependenciesRepository.saveAll(captor.capture())).thenReturn(any(Iterable.class));
        puller.loadPlatformDependencies();

        verify(dependenciesRepository,times(1)).deleteAll();
        verify(dependenciesRepository, times(1)).saveAll(any(Iterable.class));
        verify(publisher,times(1)).publishEvent(any());

   }

But there is a problem, that method pullLastDependencies() work incorect now.但是有一个问题,这个方法 pullLastDependencies() 现在工作不正确。 I have a mistake:我有一个错误:

Invalid use of argument matchers!
0 matchers expectd, 1 recorded:

Method pullLastDependencies() returns List.方法 pullLastDependencies() 返回列表。 Can I test this method without a properly working method pullLastDependencies()?我可以在没有正确工作的方法 pullLastDependencies() 的情况下测试此方法吗? Or maybe I should test this method in another way?或者我应该以另一种方式测试这种方法?

You're using the captor in when() instead of verify() .您在when()而不是verify()使用捕获器。 And you're returning any() (which is just null ) from your mocked method, instead of returning what you want this mock to return.你从你的模拟方法返回any() (它只是null ),而不是返回你想要这个模拟返回的内容。 if you don't care about what it returns because you don't use it, then return an empty iterable.如果你不关心它返回什么因为你不使用它,那么返回一个空的可迭代对象。

It should be它应该是

when(dependenciesRepository.saveAll(any()).thenReturn(Collections.emptyList());
puller.loadPlatformDependencies();

verify(dependenciesRepository).deleteAll();
verify(dependenciesRepository).saveAll(captor.capture());

I think the problem here is that you are using a matcher as a return value in我认为这里的问题是您使用匹配器作为返回值

when(dependenciesRepository.saveAll(captor.capture())).thenReturn(any(Iterable.class));

You should use the matchers to "match" method parameters, and return another structure, like this:您应该使用匹配器来“匹配”方法参数,并返回另一个结构,如下所示:

when(dependenciesRepository.saveAll(anyIterable())).thenReturn(Collections.emptyList())

As long as your pullLastDependencies() method doesn't have another dependency, it should work.只要您的 pullLastDependencies() 方法没有其他依赖项,它就应该可以工作。

Edit: It seems that your pullLastDependencies() has some other dependencies, so you need to mock the call to it.编辑:似乎您的 pullLastDependencies() 有一些其他依赖项,因此您需要模拟对它的调用。 You can achieve this by changing the visibility of the method during the test, so you can mock it, but keep in mind that this is not considered good pratice.您可以通过在测试期间更改方法的可见性来实现这一点,因此您可以模拟它,但请记住,这不被视为良好的实践。

//making private method accessible
Method method = service.getClass().getDeclaredMethod("pullLastDependencies",params);
method .setAccessible(true); 
when(pullLastDependencies()).thenReturn(Collections.emptyList())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM