zoukankan      html  css  js  c++  java
  • 实验二 单元测试

    实验目的

        (1) 用JUnit编写单元测试;
        (2) 学习代码覆盖率和性能监测工具的使用;

    实验内容

         1、 学习单元测试和代码覆盖率工具的使用

    (1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”);
    (2)编写单元测试进行测试;
    (3)用ElcEmma查看代码覆盖率,要求覆盖达到100%。

         

    package com.zhang.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 + "出现了" + wordsMap.get(o)+"次");
      }
         }
     }

    //单元测试

    package com.zhang.test;
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    import com.kai.demo.Test2;
    
    
    public class Test2Test {
    
    @Test
    public void test() {
    String strWords = "Hello World My First Unit Test";
    Test2 test = new Test2(strWords);
    test.OutputResult();
    }
    
    }

    覆盖我就不上传啦!

    2、 学习单元测试代码覆盖率工具的使用

    (1)把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;

    (2)编写单元测试进行测试;

    (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。

    (1)

    package Test1;
    
    import java.util.Scanner;
    
    public class test2{
    public static void main(String[] args) {
    
    Scanner input = new Scanner(System.in);
    
    System.out.print("请输入要测试的字符串:");
    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("颠倒顺序后的字符串:: "+result.toString());
    
    }
    }

    (2)测试:

    package Test1;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    public class test2Test {
    
        @Before
        public void setUp() throws Exception {
        }
    
        @After
        public void tearDown() throws Exception {
        }
    
        @Test
        public void testTest() throws Exception  {
            String str="how are you";
            test2.test(str);
            }
        }
  • 相关阅读:
    形象理解ERP(转)
    禁用windows server 2008 域密码复杂性要求策略
    How to adding find,filter,remove filter on display method Form
    Windows Server 2008 R2激活工具
    How to using bat command running VS development SSRS report
    Creating Your First Mac AppGetting Started
    Creating Your First Mac AppAdding a Track Object 添加一个 Track 对象
    Creating Your First Mac AppImplementing Action Methods 实现动作方法
    Creating Your First Mac AppReviewing the Code 审查代码
    Creating Your First Mac AppConfiguring the window 设置窗口
  • 原文地址:https://www.cnblogs.com/zhangwei123/p/5322370.html
Copyright © 2011-2022 走看看