当logger.error被调用时,有许多不同的东西可以作为第三个参数传递。
如何验证e包含特定子字符串?
这是我的生产代码..
public class MyClass {
private ILogger logger = Slf4jLogbackLogger
.generateLogger(MyClass.class.getClassLoader().getResource("log_messages.properties"));
public void doSomething() {
logger.info(Optional.empty(), "MyClass.doSomething");
try {
.. do things // throw new RuntimeException("something");
} catch (Exception e) {
logger.error(Optional.empty(), "HANDLE_EXCEPTION", e);
}
}
}
我的测试代码..
@RunWith(MockitoJUnitRunner.class)
public class TestMyClass {
@Mock
private ILogger logger;
@InjectMocks
@Spy
private MyClass myClass = new MyClass();
@Before
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testMyClass() throws Exception {
try {
myClass.doSomething();
} catch (Exception e) {
fail("Should not have thrown any exceptions");
}
Mockito.verify(logger, Mockito.times(1)).error(Optional.empty(), "HANDLE_EXCEPTION", "Contains something specific");
}
}
这是我收到的错误..
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
3 matchers expected, 1 recorded:
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
尝试过类似下面的事情,但同样的错误:
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
Mockito.verify(logger, Mockito.times(1)).error(Optional.empty(), "HANDLE_EXCEPTION", argument.capture());
鸿蒙传说
相关分类