字符串的 JUnit 测试
我正在为 StringUtility 类编写这些方法,但我在为它们编写一些 JUnit 测试时遇到了困难。 第一个方法是reverse():
/**
* @author Nguyen Vo
*/
public class StringUtility {
public static String reverse(String sentence){
sentence = sentence.toLowerCase();
String[] sentenceArray = sentence.split("\\s+");
String reversedSentence = "";
for (int i = sentenceArray.length - 1; i>=0; i--){
reversedSentence += sentenceArray[i] + " ";
}
return reversedSentence;
}
第二个方法是maxOccuringCharacter(),它返回字符串中出现最多的字符:
public static char maxOccuringCharacter(String sentence) throws IllegalArgumentException{
sentence = sentence.toLowerCase();
if (sentence == null || sentence.length() == 0)
throw new IllegalArgumentException();
char max_occuring_char = ' ';
int max_count = 0;
for (int i = 0; i < sentence.length(); i++) {
if (Character.isAlphabetic(sentence.charAt(i))) {
int count = 0;
for (int j = 0; j < sentence.length(); j++) {
if (sentence.charAt(i) == sentence.charAt(j))
count++;
}
if (count > max_count) {
max_occuring_char = sentence.charAt(i);
max_count = count;
}
}
}
return max_occuring_char;
}
最后一个方法是isPalindrome()。回文是向后读与向前读相同的字符序列,例如“女士”或“赛车”。
public static boolean isPalindrome(String sentence){
if (sentence.equals("")){
return true;
}
String anotherSentence = "";
for (int i = sentence.length() - 1; i >= 0; i--){
anotherSentence += sentence.charAt(i);
}
return sentence.equals(anotherSentence);
}
}
StringUtility 中的这些代码的语法是正确的,但在编写这些 JUnit 测试时遇到了一些麻烦:testReverse()、testMaxOccuringCharacterException()、textMaxOccuringCharacter() 和 testIsPalindrome()。这些至少对我来说看起来不错,但它不断显示错误。可能我在某个地方弄错了。有人可以帮我一下吗? 在此感谢您的所有帮助!
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class StringUtilityTester{
private StringUtility a;
/**
* Nguyen Vo
*/
@Before
public void setup(){
a = new StringUtility();
}
@Test
public void testReverse(){
String reversedString = a.reverse("abc");
assertEquals("cba", reversedString);
}
@Test
public void testMaxOccuringCharacter(){
assertEquals('a', a.maxOccuringCharacter("aaaaaab"));
}
@Test
public void testIsPalindrome(){
assertEquals("madam", a.isPalindrome());
}
}
I'm writing these methods for the class of StringUtility, but I get really stuck at writing some JUnit test for them.
The first method is reverse():
/**
* @author Nguyen Vo
*/
public class StringUtility {
public static String reverse(String sentence){
sentence = sentence.toLowerCase();
String[] sentenceArray = sentence.split("\\s+");
String reversedSentence = "";
for (int i = sentenceArray.length - 1; i>=0; i--){
reversedSentence += sentenceArray[i] + " ";
}
return reversedSentence;
}
The second one is maxOccuringCharacter(), which returns the max occuring char from a string:
public static char maxOccuringCharacter(String sentence) throws IllegalArgumentException{
sentence = sentence.toLowerCase();
if (sentence == null || sentence.length() == 0)
throw new IllegalArgumentException();
char max_occuring_char = ' ';
int max_count = 0;
for (int i = 0; i < sentence.length(); i++) {
if (Character.isAlphabetic(sentence.charAt(i))) {
int count = 0;
for (int j = 0; j < sentence.length(); j++) {
if (sentence.charAt(i) == sentence.charAt(j))
count++;
}
if (count > max_count) {
max_occuring_char = sentence.charAt(i);
max_count = count;
}
}
}
return max_occuring_char;
}
And the last one is isPalindrome(). A palindrome is a sequence of characters that read the same backward as forward, such as madam or racecar.
public static boolean isPalindrome(String sentence){
if (sentence.equals("")){
return true;
}
String anotherSentence = "";
for (int i = sentence.length() - 1; i >= 0; i--){
anotherSentence += sentence.charAt(i);
}
return sentence.equals(anotherSentence);
}
}
Those code's syntax from StringUtility is correct, but I get some trouble writing these JUnit testing: testReverse(), testMaxOccuringCharacterException(), textMaxOccuringCharacter(), and testIsPalindrome(). These at least looks good to me, but it keeps showing errors. Probably I got wrong somewhere somehow. Could anyone please give me a hand ?
Thanks for all your helps here!
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class StringUtilityTester{
private StringUtility a;
/**
* Nguyen Vo
*/
@Before
public void setup(){
a = new StringUtility();
}
@Test
public void testReverse(){
String reversedString = a.reverse("abc");
assertEquals("cba", reversedString);
}
@Test
public void testMaxOccuringCharacter(){
assertEquals('a', a.maxOccuringCharacter("aaaaaab"));
}
@Test
public void testIsPalindrome(){
assertEquals("madam", a.isPalindrome());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
isPalindrome(String)
接受一个字符串参数,但您在没有参数的情况下调用它 -assertEquals("madam", a.isPalindrome());
另一件事 - 您不这样做从实例中使用静态方法,就像从类本身中使用它们一样。您应该删除
a = new StringUtility();
并且应该变为
这同样适用于其余的测试。
isPalindrome(String)
accepts a string parameter, but you call it without param -assertEquals("madam", a.isPalindrome());
Another thing - you don't use static methods from instance, you use them from class itself. You should remove
a = new StringUtility();
andshould become
The same applies for the rest of your tests.
我无法对
testMaxOccuringCharacterException()
说任何话,因为您没有共享该测试。testIsPalindrome()
无法编译,因为它缺少StringUtility.isPalindrome()
方法的参数:应该检查哪个字符串是否是回文?要使测试类编译,您需要将
testIsPalindrome()
方法更改为请注意,此更改将使您的测试类编译,但
testIsPalindrome()
测试将失败。这是因为isPalindrome()
方法返回一个布尔值(true 或 false),但您的测试期望返回值是字符串。testReverse()
测试失败,因为测试验证的不是reverse()
方法所做的事情:reverse()
方法拆分输入将句子分成空格分隔的块(单词)并反转这些块(单词)的顺序(即将“hello world”更改为“world hello”)。testReverse()
尝试验证“abc”是否更改为“cba”。I can't say anything about
testMaxOccuringCharacterException()
because you didn't share that test.The
testIsPalindrome()
doesn't compile because it is missing a parameter to theStringUtility.isPalindrome()
method: which string should be checked whether it is a palindrome or not?To make the test class compile you need to change the
testIsPalindrome()
method intoPlease note that this change will make your test class compile but the
testIsPalindrome()
test will fail. This is because theisPalindrome()
method returns a boolean (either true or false), but your test expects the returned value to be a string.The
testReverse()
test fails because what the test verifies is not what thereverse()
method does:reverse()
methods splits the input sentence into space separeted chunks (words) and reverses the sequence of those chunks (words) (i.e. it changes "hello world" into "world hello").testReverse()
tries to verify that "abc" is changed to "cba".