以下测试成功运行:
assertEquals(a.toString(), b.toString());
以下不是:
assertEquals(a, b);
a
是StringBuilder
,而b
是不同类型的CharSequence
。有什么方法可以测试
CharSequence
是否相等,而无需先将它们转换为String
吗? 最佳答案
您可以使用CharBuffer
:
assertEquals(CharBuffer.wrap(a), CharBuffer.wrap(b));
CharBuffer
的.equals的javadoc保证了这一点:Tells whether or not this buffer is equal to another object.
Two char buffers are equal if, and only if,
They have the same element type,
They have the same number of remaining elements, and
The two sequences of remaining elements, considered independently of their starting positions, are pointwise equal.