如何解决JUnit assertEquals 失败显示以 [] 结尾的相同字符串
我创建了一个程序来创建和操作字符数组形式的文本行。我目前正在测试它,并在使用 JUnit assertEquals 时遇到问题。我使用 assertEquals 来测试方法,方法是将 TextLine 对象中的各个值与预期值进行比较。
对于我的前几次测试,没有任何问题并且一切正常,包括重写的 toString() 方法。但是,此测试失败(之前使用 EditableTextLine a;
声明了 a):
@Test
public void testGrowArray() {
a = new EditableTextLine("iamdoingfine");
assertEquals("toString","Text line: iamdoingfine",a.toString());
assertEquals("length",12,a.length());
assertEquals("capacity",80,a.capacity());
a.testGrowArray(100);
assertEquals("toString2",a.toString());
assertEquals("length2",100,a.length());
assertEquals("capacity2",160,a.capacity());
}
这个失败信息是这样的:
1) testGrowArray(edu.uga.cs1302.test.EditableTextLineTester)
org.junit.ComparisonFailure: toString2 expected:<...t line: iamdoingfine[]> but was:<...t line: iamdoingfine[]>
at org.junit.Assert.assertEquals(Assert.java:117)
at edu.uga.cs1302.test.EditableTextLineTester.testGrowArray(EditableTextLineTester.java:93)
具体来说,我的问题是 expected:<...t line: iamdoingfine[]> but was:<...t line: iamdoingfine[]>
。我不知道问题是什么或如何解决它。看起来某处有一个不可见的零宽度字符,但我不知道如何修复它。
toString 代码:
@Override
public String toString() {
String out = "Text line: ";
for (int i = 0; i < length; i++) {
out += txtLine[i];
}
return out;
}
这里是 growArray
方法(当前由公共方法 testGrowArray(int newLength)
调用,它只调用 growArray(newLength)
:
private void growArray(int newLength) {
//Creates a buffer with a capacity equal to the smallest multiple of
//the default size capable of holding the original line plus the
//added spaces
int multNeeded = (int) Math.ceil( (double)newLength / DEFAULT_SIZE);
char[] tempBuffer = new char[DEFAULT_SIZE * multNeeded];
setCapacity(DEFAULT_SIZE * multNeeded);
for (int i = 0; i < length(); i++) //Writes the orignal line to the buffer
tempBuffer[i] = this.txtLine[i];
this.txtLine = tempBuffer; //Sets txtLine to new,larger array
setLength(newLength);
}
DEFAULT_SIZE
是 80,如果重要的话。
另外,我在没有 toString2 断言的情况下运行了测试并且它没有失败地完成,所以 length() 和 capacity() 都运行正常。
任何帮助找出导致隐形角色出现的原因以及原因将不胜感激。我还没有找到任何其他与同一问题相关的问题,但如果有请告诉我。
编辑:我已经解决了我的问题;像往常一样,它是由我自己的脑放屁引起的。我想把这个留给未来的用户,但无论出于何种原因,我都无法在 2 天内选择我自己的答案作为解决方案,因此它将保持悬而未决。
如果有人需要,我的答案在我的评论中。
解决方法
当您从 String
创建 char[]
时(我假设这就是您在 toString
方法中所做的),String
包含数组中的每个字符。您可以使用 String(char[] but,int offset,int count)
构造函数从数组的子部分生成字符串。
试试运行这个例子:
public class NullChars {
public static void main(String... args) {
char[] buf = new char[10];
buf[0] = 'f';
buf[1] = 'o';
buf[2] = 'o';
String a = "";
// this is a terribly inefficient way of creating a String from a char[]
for (int i = 0; i < buf.length; i++) {
a += buf[i];
}
String b = new String(buf,3);
System.out.println(a);
System.out.println(a.length());
System.out.println("foo".equals(a));
System.out.println(b);
System.out.println(b.length());
System.out.println("foo".equals(b));
}
}
所以也许您的 length
在您的构造函数中设置正确,但肯定在 growArray
中您将其设置为与字符串中“真实”字符数无关的值。
解决方案/问题是我的 toString 语句正在获取数组中的空字符并打印它们。通常,那里会有其他字符,但由于我跳过它来测试growArray,所以没有放置它们。
进一步澄清:我的 toString 使用 length() 来停止连接字符。我在数组中添加了长度,但没有在那里放置字符,因此 toString 方法尝试从从未填充过的数组槽中添加字符,从而导致字符不可见。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。