【发布时间】:2017-05-28 14:19:50
【问题描述】:
我要写方法:
public Map<Robot, Integer> PickedUpForEachRobot(Set<Stuff> pickedUp)
它必须遍历作为参数传递的集合,并且必须计算每个机器人拾取的东西的数量并将其关联到他的实例。
我所做的是这样的:
public Map<Robot, Integer> PickedUpForEachRobot(Set<Stuff> pickedUp) {
final Map<Robot,Integer> map = new HashMap<>();
for(Stuff stuff : pickedUp){
Integer quantity = map.get(stuff.getPicker());
if(quantity!=null){
map.put(stuff.getPicker(), quantity);
}
}
return map;
}
我还有其他的课:
public class Stuff {
private Robot picker;
public Robot getPicker() {
return this.picker;
}
}
和:
public class Robot {
private Set<Stuff> bunchOfStuff;
public Set<Stuff> getBunchOfStuff() {
return this.bunchOfStuff;
}
}
我试图合成,所以我希望我能清楚。
所以我的问题是,当我对此方法进行测试时:
@Test
public void testRaccoltoPerMezzo() {
Statistics stats = new Statistics();
Stuff stuff1 = new ball();
Stuff stuff2 = new legoPiece();
Set<Stuff> set = new HashSet<>();
set.add(stuff1);
assertEquals(1,set.size());
Map<Robot,Integer> map = new HashMap<>();
map.put(stuff1.getPicker(),1);
assertEquals(map, stats.PickedUpForEachRobot(set));
}
它失败了,它对我说:
java.lang.AssertionError: expected:<{null=1}> but was:<{}>
我不明白为什么。有人可以帮帮我吗?
【问题讨论】:
标签: java unit-testing hashmap hashset