gpt4 book ai didi

java - 有没有办法让 never() +startsWith() 匹配器报告匹配的内容?

转载 作者:太空宇宙 更新时间:2023-11-04 06:15:45 25 4
gpt4 key购买 nike

我有这个:

verify(logger, never()).info(startsWith("Created content from "));

错误信息是这样的:

org.mockito.exceptions.verification.NeverWantedButInvoked: 
logger.info(
startsWith("Created content from ")
);

因为我使用的是 startsWith,所以了解完整的字符串对于调试此故障非常有帮助。有什么方法可以获取该字符串吗?

最佳答案

我认为您可以做到这一点的唯一方法是编写您自己的匹配器。您可以在自己的实用程序类中添加一个返回匹配器的方法,例如...

public class CustomMatchers {
public static String capturingStartsWith(final String expected) {
return Mockito.argThat(new TypeSafeMatcher<String>() {

private String actual;

@Override
public void describeTo(Description description) {
description.appendText("String that starts with: ");
description.appendValue(expected);
description.appendText(" actual: ");
description.appendValue(actual);
}

@Override
protected boolean matchesSafely(String actual) {
this.actual = actual;
return actual.startsWith(expected);
}
});
}
}

然后你的测试...

import static CustomMatchers.*;
...
@Test
public void shouldNotDoWhatItDoesNow() {
classUnderTest.doStuff();

verify(logger, never()).info(capturingStartsWith("Created content from"));
}

这会给你这样的输出......

org.mockito.exceptions.verification.NeverWantedButInvoked: 
logger.info(
Start that starts with: "Created content from" actual: "Created content from [WIBBLE]"
);
Never wanted here:
-> at CustomStartsWithTest.shouldNotDoWhatItDoesNow(CustomStartsWithTest.java:28)
But invoked here:
-> at CustomStartsWith.doStuff(CustomStartsWith.java:12)

(不将类型安全匹配器包装在辅助方法中的“argThat”中并返回 Matcher 可能会更优雅)

我不知道有任何免费库有这样的额外匹配器。可能有一种更聪明的方法来破解 Mockito 来为你做到这一点......

关于java - 有没有办法让 never() +startsWith() 匹配器报告匹配的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28117461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com