junit测试用于排序阵列列表
因此,我有方法:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建地图。在这种情况下,我添加了一个具有重复值的元素。
现在调用该方法
现在根据我们期望处理地图的处理方式测试顺序。对于Out Map,键的顺序应为“一个”,“两个”,“三”,“另一个”,“四”,并且值的顺序应为1、2、3、4、4。
Create the Map. In this case I'm adding an element that has a duplicated value.
Now invoke the method
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.
地图有更方便的方法。
There is more convenient way for map.