Learn the use of unit tests and code coverage tools
(1) Write a program that parses the frequency of occurrences of each word in a string and displays the word and the frequency at which it appears. (the words are separated by a space, such as "Hello World My First Unit Test");
Import java.util.*;
public class Main {
public void Countword (String str) {
string[] Wordsarray = Str.split ("");
map<string, integer> wordsmap = new hashmap<string, integer> ();
for (String Word:wordsarray) {
if (Wordsmap.containskey (word)) {
Wordsmap.put (Word, wordsmap.get (word) + 1);
}
else {
Wordsmap.put (Word, 1);
}
}
set<string> Setkey = Wordsmap.keyset ();
iterator<string> Itkey = Setkey.iterator ();
while (Itkey.hasnext ()) {
String Word = Itkey.next (). toString ();
int count = Wordsmap.get (word);
System.out.println (" word " + word + " occurrences " + count + " times ");
}
}
}
(2) Writing unit tests for testing;
Import static org.junit.assert.*;
Import Org.junit.Test;
public class Demotext {
@Test
public void Test () {
Main demo = new Main ();
Demo.countword ("Hello World hello Java Hello Test");
}
}
(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.
3 Learning Unit test the use of code coverage tools
(1) The order of words in an English sentence is reversed and then output. For example, enter "How IS is", output "you is how";
public class Text1 {
public void reverse (String str) {
string[] Wordsarray = Str.split ("");
System.out.print (" reverse word output :");
for (int i = wordsarray.length-1; I >= 0; i--) {
System.out.print (Wordsarray[i] + "");
}
}
}
(2) Writing unit tests for testing;
Import static org.junit.assert.*;
Import Org.junit.Test;
public class Text {
@Test
public void Test () {
Text1 Text3 = new Text1 ();
Text3.reverse ("Hello World hello Java Hello Test");
}
}
(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.
Lab Two Unit Test