Learn the use of unit tests and code coverage tools

Source: Internet
Author: User

(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%.

Package Com.kai.demo;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Set;

public class Test2 {


Private map<string, integer> Wordsmap;
Public Test2 (String strwords) {
Wordsmap = This.getarray (strwords);
}

Public map<string, integer> GetArray (String strwords) {
string[] Words_array = Strwords.split ("");
map<string, integer> words_map = new hashmap<string, integer> ();

int arrlength = Words_array.length;
for (int i = 0; i < arrlength; i++) {
if (!words_map.containskey (Words_array[i])) {
Words_map.put (Words_array[i], 1);
} else {
int currentnum = Words_map.get (Words_array[i]) + 1;
Words_map.remove (Words_array[i]);
Words_map.put (Words_array[i], currentnum);
}
}
return words_map;
}

public void Outputresult () {
Set s = wordsmap.keyset ();
Iterator i = S.iterator ();
while (I.hasnext ()) {
Object o = I.next ();
SYSTEM.OUT.PRINTLN (o + " appears " + wordsmap.get (o) + " times ");
}
}
}

Learning the use of unit tests and code coverage tools

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.