junit -- 验证 LOGGER 而不模拟



在我的功能测试中,我不想模拟LOGGER对象,而是想在错误的情况下验证它。以下是几行代码:

private static final Logger LOGGER_OF_ANOTHER_CLASS = Logger.getLogger(DataProvider.class);
@Before public void setup() { Mockito.spy(LOGGER_OF_ANOTHER_CLASS); }
@Test public void testWarningIsLoggedWhenNoDataExists() {
    whenInvalidCriteriaIsPassed();    // This is private method 
    Mockito.verify(LOGGER_OF_ANOTHER_CLASS, times(1)).warn(new ArgumentMatcher<String>() {
        @Override
        public boolean matches(Object argument) {
            String message = (String) argument;
            return message.startsWith("Unable to fetch data for criteria:");
        }
    });
}

对此,我得到了例外

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is not a mock!
Examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();

我尝试了几个在StackOverflow上搜索的场景,但失败了。

我认为例外是当场的。一个小的更改可能会解决您的测试问题。

private static final Logger LOGGER_OF_ANOTHER_CLASS = Mockito.spy(Logger.getLogger(DataProvider.class));

相关内容

  • 没有找到相关文章

最新更新