英文:
JUnit test for Anagram making program?
问题
public String reverseWords(String input) {
String oneOrMoreSpaces = "\\s+";
String[] words = input.split(oneOrMoreSpaces);
String completeAnagram = "";
for (int i = 0; i < words.length; i++) {
char[] symbol = words[i].toCharArray();
char newSymbolSequence;
int j = symbol.length - 1, k = 0;
while (k < j) {
if (!Character.isAlphabetic(symbol[k]))
k++;
else if (!Character.isAlphabetic(symbol[j]))
j--;
else {
newSymbolSequence = symbol[k];
symbol[k] = symbol[j];
symbol[j] = newSymbolSequence;
k++;
j--;
}
}
completeAnagram = new String(symbol);
}
return completeAnagram;
}
private Anagram anagram = new Anagram();
@Test(expected = NullPointerException.class)
public void checkExpected_NullPointerException() {
anagram.reverseWords(null);
}
@Test
public void reverseWords_shouldMakeAnagram_ofInputText() {
String actual = anagram.reverseWords("qwe123rty");
String expected = "ytr123ewq";
assertEquals(expected, actual);
}
@Test
public void throwingTest() {
String actual = anagram.reverseWords("qwe123rty");
String expected = "qwe123rty";
assertNotSame(expected, actual);
}
If you need additional @Test
methods, you could consider adding tests for some edge cases and special scenarios:
@Test
public void reverseWords_emptyInput() {
String actual = anagram.reverseWords("");
String expected = "";
assertEquals(expected, actual);
}
@Test
public void reverseWords_singleWord() {
String actual = anagram.reverseWords("hello");
String expected = "olleh";
assertEquals(expected, actual);
}
@Test
public void reverseWords_multipleSpaces() {
String actual = anagram.reverseWords(" multiple spaces ");
String expected = " elpitonum secaps ";
assertEquals(expected, actual);
}
@Test
public void reverseWords_specialCharacters() {
String actual = anagram.reverseWords("hello@world!");
String expected = "dlrow@olleh!";
assertEquals(expected, actual);
}
These additional test cases cover various scenarios and help ensure the robustness of your reverseWords
method.
英文:
I have to write junit tests for this part of code: but I have to tests everything, means not only assertEquals(expected, actual)
, but also all exceptions which could be, all functionality.
public String reverseWords(String input) {
String oneOrMoreSpaces = "\\s+";
String[] words = input.split(oneOrMoreSpaces);
String completeAnagram = "";
for (int i = 0; i < words.length; i++) {
char[] symbol = words[i].toCharArray();
char newSymbolSequence;
int j = symbol.length - 1, k = 0;
while (k < j) {
if (!Character.isAlphabetic(symbol[k]))
k++;
else if (!Character.isAlphabetic(symbol[j]))
j--;
else {
newSymbolSequence = symbol[k];
symbol[k] = symbol[j];
symbol[j] = newSymbolSequence;
k++;
j--;
}
}
completeAnagram = new String(symbol);
}
return completeAnagram;
}
This is what I've already made
private Anagram anagram = new Anagram();
@Test(expected = NullPointerException.class)
public void checkExpected_NullPointerException(){
anagram.reverseWords(null);
}
@Test
public void reverseWords_shouldMakeAnagram_ofInputText(){
String actual = anagram.reverseWords("qwe123rty");
String expected = "ytr123ewq";
assertEquals(expected, actual);
}
@Test
public void throwingTest(){
String actual = anagram.reverseWords("qwe123rty");
String expected = "qwe123rty";
assertNotSame(expected, actual);
}
Can anyone help me with good @Test
methods which really should be there. I have no idea what I can @Test
else.
答案1
得分: 2
测试一些极限值:
String actual = anagram.reverseWords("");
String actual = anagram.reverseWords(" ");
String actual = anagram.reverseWords("q");
你正在对单词进行一些逻辑操作,所以也要测试这个:
String actual = anagram.reverseWords("more than a feeling");
String actual = anagram.reverseWords("azerty querty");
String actual = anagram.reverseWords("azerty ");
String actual = anagram.reverseWords(" azerty ");
提示:我认为你会发现一些错误,因为`completeAnagram`每次被覆盖,只包含最后一个单词。
英文:
Test some limit values:
String actual = anagram.reverseWords("");
String actual = anagram.reverseWords(" ");
String actual = anagram.reverseWords("q");
You're doing some logic with words so test this too:
String actual = anagram.reverseWords("more than a feeling");
String actual = anagram.reverseWords("azerty querty");
String actual = anagram.reverseWords("azerty ");
String actual = anagram.reverseWords(" azerty ");
spoiler: I think you will discover some bugs as completeAnagram
is each time overwritten and only contains the last word.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论