Java 如何测试LinkedHashMap的值和键?

Java 如何测试LinkedHashMap的值和键?,java,testing,junit,Java,Testing,Junit,我编写了一个方法,该方法将字符串的字符作为键放入LinkedHashMap,并将该字符的数量作为值,但我不知道如何测试该方法是否正确计算字符数 LinkedHashMap=新建LinkedHashMap(); 对于(int i=0;i

我编写了一个方法,该方法将字符串的字符作为键放入LinkedHashMap,并将该字符的数量作为值,但我不知道如何测试该方法是否正确计算字符数

LinkedHashMap=新建LinkedHashMap();
对于(int i=0;i
输入示例:“abc de”


作为测试,我想检查是否有一个字符“a”,其数量为1。

以下是您案例的测试示例:

package your.package.here;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsMapContaining.hasEntry;

import java.util.Map;
import org.junit.Test;

public class MainTest {

  @Test
  public void testMapContent() {
    String input = "abc de";
    Integer aExpectedCount = 1;

    Map<Character, Integer> generatedMap = Main.generateMap(input);

    assertThat(generatedMap, hasEntry('a', aExpectedCount));
  }
}
在此处打包您的.package.here;
导入静态org.hamcrest.matcherasert.assertThat;
导入静态org.hamcrest.collection.IsMapContaining.hasEntry;
导入java.util.Map;
导入org.junit.Test;
公共类测试{
@试验
public void testMapContent(){
字符串输入=“abc de”;
整数AEExpectedCount=1;
Map generatedMap=Main.generateMap(输入);
资产(generatedMap,hasEntry('a',AeExpectedCount));
}
}
主要课程包括:

package your.package.here;

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {  

  public static Map<Character, Integer> generateMap(String string) {
    LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
    for (int i = 0; i < string.length(); i++) {
      char wordToLetter = string.toLowerCase().charAt(i);
      if (map.containsKey(wordToLetter)) {
        int quantity = map.get(wordToLetter);
        map.put(wordToLetter, ++quantity);
      } else {
        map.put(wordToLetter, 1);
      }
    }
    return map;
  }
}
在此处打包您的.package.here;
导入java.util.LinkedHashMap;
导入java.util.Map;
公共类主{
公共静态映射生成器映射(字符串){
LinkedHashMap=新建LinkedHashMap();
对于(int i=0;i
这里有一种测试字符的方法。它不使用Junit,但可以很容易地进行调整

Random r = new Random();
// save the seed for repeated testing below
long seed = r.nextLong();
r.setSeed(seed);

// supplier to get the next count of letters between 5 and 14 chars
Supplier<Integer> rnd = () -> r.nextInt(10) + 5;
现在调用您的方法来获取数据并进行计数

Map<Character, Integer> freq = countChars(testString);
它打印的计数方法

Count for 'a' passes
Count for 'b' passes
Count for 'c' passes
Count for 'd' passes
Count for 'e' passes
Count for 'f' passes
Count for 'g' passes

您的代码放入了一个方法

public static Map<Character, Integer> countChars(String string) {
    LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
    for (int i = 0; i < string.length(); i++) {
        char wordToLetter = string.toLowerCase().charAt(i);
        if (map.containsKey(wordToLetter)) {
            int quantity = map.get(wordToLetter);
            map.put(wordToLetter, ++quantity);
        } else {
            map.put(wordToLetter, 1);
        }
    }
     return map;
}
公共静态映射countChars(字符串){
LinkedHashMap=新建LinkedHashMap();
对于(int i=0;i
下面给出了一种测试方法:

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args) {
        LinkedHashMap<Character, Integer> charCountMap = output("abc de");
        assertEquals(1, charCountMap.get('a'));
        assertEquals(1, charCountMap.get('b'));
        assertEquals(1, charCountMap.get(' '));
    }

    public static LinkedHashMap<Character, Integer> output(String inputString) {
        LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < inputString.length(); i++) {
            char wordToLetter = inputString.toLowerCase().charAt(i);
            if (map.containsKey(wordToLetter)) {
                int quantity = map.get(wordToLetter);
                map.put(wordToLetter, ++quantity);
            } else {
                map.put(wordToLetter, 1);
            }
        }
        return map;
    }
}
导入静态org.junit.jupiter.api.Assertions.assertEquals;
导入java.util.LinkedHashMap;
公共班机{
公共静态void main(字符串[]args){
LinkedHashMap charCountMap=输出(“abc de”);
assertEquals(1,charCountMap.get('a'));
assertEquals(1,charCountMap.get('b'));
assertEquals(1,charCountMap.get(“”));
}
公共静态LinkedHashMap输出(字符串输入字符串){
LinkedHashMap=新建LinkedHashMap();
对于(int i=0;i
如果您输入不同的值,例如,
assertEquals(2,charCountMap.get('a')),它将抛出断言失败错误
异常。

无JUnit的测试方法 检查方法的方法是从另一个方法调用它,并将结果与某些验证方法或预先已知的预期值进行比较。在本例中,如果没有junit,它可能如下所示:

公共类测试{
公共静态void main(字符串[]args){
String str=“abc de”;
Map Map=stringToMap(str);
Map mapTest=stringToMapTest(str);
//视觉比较
System.out.println(map);//{a=1,b=1,c=1,=1,d=1,e=1}
System.out.println(mapTest);//{a=1,b=1,c=1,=1,d=1,e=1}
//自动比较
System.out.println(map.equals(mapTest));//true
//逐步比较
System.out.println(map.getOrDefault('a',0).equals(1));//true
System.out.println(map.getOrDefault('b',0).equals(1));//true
}
私有静态映射stringToMap(stringstr){
LinkedHashMap=新建LinkedHashMap();
对于(int i=0;i(char)ch)
.collect(collector.toMap)(
ch->ch,
ch->1,
整数::和,
LinkedHashMap::new));
}
}

您可以使用字符串输入(如“abc de”)调用此函数,并在测试中添加断言语句,以检查返回的映射中每个字符的质量是否正确。
public static Map<Character, Integer> countChars(String string) {
    LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
    for (int i = 0; i < string.length(); i++) {
        char wordToLetter = string.toLowerCase().charAt(i);
        if (map.containsKey(wordToLetter)) {
            int quantity = map.get(wordToLetter);
            map.put(wordToLetter, ++quantity);
        } else {
            map.put(wordToLetter, 1);
        }
    }
     return map;
}
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.LinkedHashMap;

public class Main {
    public static void main(String[] args) {
        LinkedHashMap<Character, Integer> charCountMap = output("abc de");
        assertEquals(1, charCountMap.get('a'));
        assertEquals(1, charCountMap.get('b'));
        assertEquals(1, charCountMap.get(' '));
    }

    public static LinkedHashMap<Character, Integer> output(String inputString) {
        LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < inputString.length(); i++) {
            char wordToLetter = inputString.toLowerCase().charAt(i);
            if (map.containsKey(wordToLetter)) {
                int quantity = map.get(wordToLetter);
                map.put(wordToLetter, ++quantity);
            } else {
                map.put(wordToLetter, 1);
            }
        }
        return map;
    }
}