Lab Two Unit Test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.