junit测试用于排序阵列列表

发布于 2025-02-01 14:30:33 字数 1005 浏览 7 评论 0原文

因此,我有方法:

public List<Map.Entry<String, Integer>> sortMap(HashMap<String, Integer> map) {
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        list.sort((o1, o2) -> {
            int value = o2.getValue().compareTo(o1.getValue());
            if (value != 0) {
                return value;
            }
            return o1.getKey().compareTo(o2.getKey());
        });
        return list;
    }

我需要为他进行Junit测试。实际上,我启动了代码,但我不知道如何正确完成它

       @Test
        public void shouldReturnCurrentList() {
            Object object = new Object();
                List<Map.Entry<String, Integer>> testList = new ArrayList<>(testMap.entrySet());
                testMap.put("One", 1);
                testMap.put("Two", 2);
                testMap.put("Three", 3);
                testMap.put("Four", 4);
        
                object.sortMap(testMap); 
}

so, I have method:

public List<Map.Entry<String, Integer>> sortMap(HashMap<String, Integer> map) {
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        list.sort((o1, o2) -> {
            int value = o2.getValue().compareTo(o1.getValue());
            if (value != 0) {
                return value;
            }
            return o1.getKey().compareTo(o2.getKey());
        });
        return list;
    }

I need a JUnit test for him. Actually, I started code it, but i dont know how to finish it right

       @Test
        public void shouldReturnCurrentList() {
            Object object = new Object();
                List<Map.Entry<String, Integer>> testList = new ArrayList<>(testMap.entrySet());
                testMap.put("One", 1);
                testMap.put("Two", 2);
                testMap.put("Three", 3);
                testMap.put("Four", 4);
        
                object.sortMap(testMap); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救赎№ 2025-02-08 14:30:33

创建地图。在这种情况下,我添加了一个具有重复值的元素。

  Map<String,Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  map.put("Three", 3);
  map.put("Four", 4);
  map.put("AnotherFour", 4);

现在调用该方法

List<Map.Entry<String, Integer>> actual = SUT.sortMap(map);

现在根据我们期望处理地图的处理方式测试顺序。对于Out Map,键的顺序应为“一个”,“两个”,“三”,“另一个”,“四”,并且值的顺序应为1、2、3、4、4。

Assert.assertEquals("One", actual.get(0).getKey());
Assert.assertEquals(1, actual.get(0).getValue()); 
Assert.assertEquals("Two", actual.get(1).getKey());
Assert.assertEquals(2, actual.get(1).getValue()); 
Assert.assertEquals("Three", actual.get(2).getKey());
Assert.assertEquals(3, actual.get(2).getValue()); 
Assert.assertEquals("AnotherFour", actual.get(3).getKey());
Assert.assertEquals(4, actual.get(3).getValue()); 
Assert.assertEquals("Four", actual.get(4).getKey());
Assert.assertEquals(4, actual.get(4).getValue()); 

Create the Map. In this case I'm adding an element that has a duplicated value.

  Map<String,Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  map.put("Three", 3);
  map.put("Four", 4);
  map.put("AnotherFour", 4);

Now invoke the method

List<Map.Entry<String, Integer>> actual = SUT.sortMap(map);

Now test the order based on how we expect the Map to be processed. For out map, the order of the keys should be "One", "Two", "Three", "AnotherFour", "Four" and the order of the values should be 1, 2, 3, 4, 4.

Assert.assertEquals("One", actual.get(0).getKey());
Assert.assertEquals(1, actual.get(0).getValue()); 
Assert.assertEquals("Two", actual.get(1).getKey());
Assert.assertEquals(2, actual.get(1).getValue()); 
Assert.assertEquals("Three", actual.get(2).getKey());
Assert.assertEquals(3, actual.get(2).getValue()); 
Assert.assertEquals("AnotherFour", actual.get(3).getKey());
Assert.assertEquals(4, actual.get(3).getValue()); 
Assert.assertEquals("Four", actual.get(4).getKey());
Assert.assertEquals(4, actual.get(4).getValue()); 
梦在夏天 2025-02-08 14:30:33

地图有更方便的方法。

assertThat(testList)
    .contains(Assertions.entry("one", 1), Assertions.entry("two", 2));

There is more convenient way for map.

assertThat(testList)
    .contains(Assertions.entry("one", 1), Assertions.entry("two", 2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文