我正在测试一个从接口实现的类。 在我的测试类中,我创建了具有名称和银行帐户值的对象(Humains)。
我的一个测试方法是使用一个arrayList并与另一个列表进行比较,如果该人已经存在,则会添加该数量。
代码是这样的: 测试类
HumainImpl<Humain> humain = new HumainImpl<Humain>();
private Humain h1 = new Humain("AAA" , 2200);
private Humain h2 = new Humain("DDD" , 500);
@Test
public void testAddAmount(){
List<Humain> testing = new ArrayList<Humain>();
testing.add(h1);
testing.add(h2);
humain.placeList(testing);
}
placeList(testing)方法在HumainImpl类中,该类调用Humain类中的另一个方法来执行添加。 在HumainImpl类中,还有一个名为implList的列表。 一旦调用此方法placeList,测试类中h1和h2的数字就会发生变化。 我想要的是,只有implList的数字才能改变。
在将h1和h2作为参数传递时,在添加数字或进行更改时,它们会受到影响吗?
我尝试了太多方法而且我没有得到它。我尝试通过作为参数传递的列表将该列表中的元素复制到另一个attribut并执行添加,但它没有帮助。
当我测试另一个将元素作为attribut,place(h1)的类似方法时,这很好用。
测试类:
HumainImpl<Humain> humain = new HumainImpl<Humain>();
private Humain h1 = new Humain("AAA" , 2200);
private Humain h2 = new Humain("DDD" , 500);
@Test
public void testAddAmount(){
List<Humain> testing = new ArrayList<Humain>();
testing.add(h1);
testing.add(h2);
humain.placer(testing); //placer(testing) is the method placer with a list parameter
}
HumainImpl:
private boolean existe(T elt){
boolean result = false;
index = uneListe.indexOf(elt);
if(index >= 0){
result = true;
}
return result;
}
@Override
public boolean placer(T elt) {
boolean result = false;
boolean found = false;
int i = -1;
int nbExemple;
if(elt != null){
boolean existe = existe(elt);
nbExemple = nbExemplaires(elt);
if(uneListe.size() > 0 && existe){
while(!found && i < uneListe.size()){
i++;
if(elt.equals(uneListe.get(i))){
found = true;
}
}
T element = uneListe.get(i);
nbExemple = element.retournerNbExemplaires();
elt.additionner(nbExemple);
result = false;
} else if(!existe && nbExemple >= 1 &&
uneListe.size() < nbPlacesMaximum) {
uneListe.add(elt);
result = true;
}
}
return result;
}
@Override
public void placer(List<T> liste) {
iterateur(liste);
while(liste != null && liste.size() > 0 && itList.hasNext()){
placer(itList.next());
}
}
在班级Humain:
@Override
public void additionner(int nbExemplaires) {
this.nbExemplaires += nbExemplaires;
}
@Override
public int retournerNbExemplaires() {
return nbExemplaires;
}