0

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

所以我想出了这个(该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 内部的某个地方,这个故障数字一定是计算出来的……有没有办法为用户打印出来?

4

1 回答 1

1

像这样添加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>
于 2017-02-22T20:46:57.757 回答