首页 > 解决方案 > 比较 JUnitTesting 中的 HashMap

问题描述

我必须:

  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;
    }
}

现在我必须设计这个的单元测试,但我在比较 hashmap 时有点困惑,请帮助我,

这是我所做的一个小尝试:

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);


        }
    }
}

我不知道 assertEqual 函数在 Map 中是如何工作的。

标签: javaunit-testinghashmapjunit5

解决方案


我已经尝试过自己,这个解决方案对我有用!

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()));


        }
    }
}

推荐阅读