gpt4 book ai didi

java - AssertJ结果反馈为 "iterable asserts"

转载 作者:行者123 更新时间:2023-12-01 08:51:18 25 4
gpt4 key购买 nike

我正在检查字符串列表是否包含满足特定条件的特定数字,特别是它们是否包含子字符串,忽略大小写。

所以我想出了这个( failedResults 字段的原因将变得显而易见):

    class ContainsIgnoringCase extends Condition<CharSequence> {
private String lCString;
private int failedResults = 0;
int getFailedResults(){
return failedResults;
}

ContainsIgnoringCase( String string ){
lCString = string.toLowerCase();
}

@Override
public boolean matches( CharSequence value ) {
boolean result = value.toString().toLowerCase().contains( lCString );
if( ! result ){
failedResults++;
}
return result;
}
}

如果失败:

    assertThat( paramsPassed ).haveExactly( 7, new ContainsIgnoringCase( "documents" ));

它打印出 List<String> paramsPassed ,但没有说明实际的失败次数。

我认为这可能会成功,但事实并非如此:

    ContainsIgnoringCase cic = new ContainsIgnoringCase( "documents" );
assertThat( paramsPassed ).as( "failed: " + cic.getFailedResults() ).haveExactly( 7, new ContainsIgnoringCase( "documents" ));

...这只是打印“失败0”,所以显然方法 getFailedResults()在测试执行之前调用。在 AssertJ 内部的某个地方,这个失败数字一定是被计算出来的……有没有办法为用户打印出来?

最佳答案

toString 添加到您的条件中,例如:

    @Override
public String toString() {
return "elements containing '" + this.lCString + "' but it failed for " + getFailedResults();
}

给定 paramsPassed = list("documents", "2"); 它将给出如下错误:

Expecting elements:
<["documents", "2"]>
to have exactly 7 times <elements containing 'documents' but failed = 1>

关于java - AssertJ结果反馈为 "iterable asserts",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42385756/

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