在JUnit中测试CharSequences是否相等

时间:2016-03-10 17:21:18

标签: java junit

以下测试成功运行:

assertEquals(a.toString(), b.toString());

以下内容不是:

assertEquals(a, b);

aStringBuilder,而b是不同类型的CharSequence

有没有办法测试CharSequence s的相等性,而不首先将它们转换为String

1 个答案:

答案 0 :(得分:2)

您可以使用CharBuffer s:

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.
相关问题