簡體   English   中英

比較 JUnitTesting 中的 HashMap

[英]Comparing HashMap in JUnitTesting

我必須:

  1. 編寫一個函數,它接受一個字符串數組/列表,計算每個字符串的出現次數,然后返回一個包含每個唯一字符串及其出現次數的數據結構。 例如,給定一個包含以下內容的輸入:

[“蘋果”、“蝙蝠”、“蘋果”、“汽車”]

該函數應返回一個數據結構,其中“apple”的計數為 2,“bat”的計數為 1,“car”的計數為 1。

  1. 為我的解決方案編寫單元測試覆蓋率。 涵蓋確保正/負功能正確的排列。 同樣,涵蓋有意義的邊緣情況以確保正確的功能並確保代碼執行沒有錯誤。

現在,我已經完成了任務一,這是代碼:

package Testing;
import java.util.*;
class CountString{
public static void main(String []args){
    List strings = Arrays.asList("appLe","b at","APPle","car!");
    Map<String,Integer> datastructure = countStrings(strings);
    for (Map.Entry<String, Integer> entry : datastructure.entrySet()) {
        System.out.println(entry.getKey() + " : " + entry.getValue());
    }
}

public static Map<String,Integer>countStrings(List<String> words){
    Map<String,Integer> map = new HashMap<>();
    List<String>newList = new ArrayList<>();
    String regex = "abcdefghijklmnopqrstuvwxyz";
    regex+=regex.toUpperCase();
    String w;
    for(String s:words){
        w="";
        for(int i=0;i<s.length();i++){
            if(regex.contains(s.charAt(i)+"")){
                w+=s.charAt(i)+"";
            }
        }
        newList.add(w.toLowerCase());
    }
    String countedWords = "";
    int count;
    for(int i=0;i<newList.size();i++){
        count=1;
        if(!countedWords.contains(newList.get(i))){
            for(int j=i+1;j<newList.size();j++){
                if(newList.get(i).equals(newList.get(j))){
                    count++;
                }
            }
            countedWords+=newList.get(i)+"";
            map.put(newList.get(i),count);
        }

    }
    return map;
    }
}

現在我必須設計這個單元測試,但我在比較哈希圖時有點困惑,請幫助我,

這是我做過的一個小嘗試:

package Testing;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;

class JUnitTest {

@Test
void test() {
    CountString test = new CountString();
    List strings = Arrays.asList("appLe","b at","APPle","car!");
    Map<String,Integer> output = test.countStrings(strings);

    for(Map.Entry<String, Integer> entry : output.entrySet()) {
        assertEquals(expecteds, output);


        }
    }
}

我不知道如何在 Map 中使用 assertEqual 函數。

我已經嘗試過自己,這個解決方案對我有用!

package Testing;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

class JUnitTest {

@Test
void test() {
    CountString test = new CountString();
    List strings = Arrays.asList("appLe","b at","APPle","car!","truck!","tr UcK2");
    Map<String,Integer> output = test.countStrings(strings);
    Map<String,Integer> expecteds = new HashMap<>();
    expecteds.put("apple", 2);
    expecteds.put("bat", 1);
    expecteds.put("car", 1);
    expecteds.put("truck", 2);
    for(Map.Entry<String, Integer> entry : output.entrySet()) {
        assertEquals(expecteds.get(entry.getKey()), output.get(entry.getKey()));


        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM