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());
}
}
CodePudding user response:
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();
and
String reversedString = a.reverse("abc");
should become
String reversedString = StringUtility.reverse("abc");
The same applies for the rest of your tests.
CodePudding user response:
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 the StringUtility.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 into
@Test
public void testIsPalindrome() {
assertEquals("madam", StringUtility.isPalindrome("madam"));
}
Please note that this change will make your test class compile but the testIsPalindrome()
test will fail. This is because the isPalindrome()
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 the reverse()
method does:
- The
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"). - The
testReverse()
tries to verify that "abc" is changed to "cba".