2. 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");
(2) Writing unit tests for testing;
(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.
(1)
Package Cn.bao;
Public class Action {
Public void Findword (String str) {//Divide words
String[] Arrayword =str.split ("");
Action A = new action ();
A.frequency (Arrayword);
}
Public void frequency (string[] arrayword) {
string[] Word = new string[arrayword.length];//to store traversed words
int time[]=new int[arrayword.length];//holds record word occurrences
Boolean t = true;
for (int i=0;i<arrayword.length;i++) {
for (int j=0;j<arrayword.length;j++) {//Traverse a list of words that have been traversed
if (Arrayword[i].equals (Word[j])) {
System. out. println ("Duplicate word!!! ");
T=false;//If the word repeats, skip
}
}
if (t==true) {//Word initial number is 1
Word[i]=arrayword[i];
Time[i]=1;
for (int j=i+1;j<arrayword.length;j++) {//Traversal string, number of records
if (Arrayword[i].equals (Arrayword[j])) {
time[i]++;
}
}
}
}
for (int i = 0;i<arrayword.length;i++) {//Traversal output count
if (word[i]!=null) {
System. out. println ("Word:" +word[i]+ "appeared" +time[i]+ "Times. ");
}
}
}
}
Package Cn.bao;
Public class Main {
/**
* @param args
*/
Public Static void Main (string[] args) {
TODO auto-generated Method stub
View v = new view ();
}
}
Package Cn.bao;
import Java.util.Scanner;
Public class View {
Public View () {
Scanner input =new Scanner (System. in);
System. out. println ("Please enter string:");
String Str=input.nextline ();
Action A = new action ();
A.findword (str);
}
}
(2)
Package Cn.bao;
Import Static Org.junit.Assert. *;
import Org.junit.Before;
import org.junit.Test;
Public class actiontest {
@Before
Public void setUp () throws Exception {
}
@Test
Public void Test () {
String str= "Hello World My first Unit Test";
Action action =New action ();
Action.findword (str);
}
}
3. learning the use of Unit test 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";
(2) Writing unit tests for testing;
(3) Use Elcemma to view code coverage, requiring coverage to reach 100%.
(1)
Package cn.li;
import Java.util.Scanner;
Public class Test2 {
/**
* @param args
*/
Public Static void Main (string[] args) {
TODO auto-generated Method stub
Scanner input = new Scanner (System. in);
System. out. Print ("Please enter the string to test:");
String str = input.nextline ();
Test2. Test (str);
}
Public Static void Test (String str) {
string[] Strarr = Str.split ("\\s+|[,]");
StringBuffer result = new stringbuffer ();
for (int i = strarr.length-1;i >=0; i--) {
Result.append (Strarr[i] + "");
}
Result.setcharat (Str.length ()-0, (char) 0);
System. out. println ("inverted string after Order::" +result.tostring ());
}
}
(2)
Package cn.li;
Import Static Org.junit.Assert. *;
import Org.junit.Before;
import org.junit.Test;
Public class Test2test {
@Before
Public void setUp () throws Exception {
}
@Test
Public void Test () throws Exception {
String str= "How is You";
Test2. Test (str);
}
}
Software Test second Job