【发布时间】:2021-09-12 10:03:01
【问题描述】:
谁能告诉我如何在下面的代码中编写测试用例
public static String reverseWord(String str) {
String a[] = str.split(" ");
try {
for (int i = a.length - 1; i >= 0; i--) {
System.out.print(a[i] + " ");
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
我已经写了下面的测试用例,但是失败了
@Test
public void testReverseWord() {
assertEquals("world hello", Logic.reverseWord("hello world"));
}
【问题讨论】:
-
你的 reverseWord 方法没有改变返回值
str,而不是打印数组,而是创建一个新的字符串实例并将其添加到那里 -
@Bahij.Mik 我不明白你在说什么,你能在代码中告诉我吗?