My add to hashtable method fails, what have i done wrong? Or what have i missunderstood?
test:
@Test
public void testAddKeyValue() {
AdminController cont = new AdminController();
Apartment o1 = new Apartment(1, 4, "Maier B", true);
ArrayList<Expense> exp = new ArrayList<>();
cont.addKeyWithList(o1, exp);
assertTrue(cont.isEmpty()); // ISSUE > the test works if it is true, but it is supposed be False.
}
repo class:
public class Repository extends HashMap<Apartment, ArrayList<Expense>>{
private Map<Apartment,ArrayList<Expense>> dic; // last expense object refers to curret month
Iterator<Map.Entry<Apartment, ArrayList<Expense>>> it;
public void addKeyWithList(Apartment apt, ArrayList<Expense> exp){
dic.put(apt, exp);
}
}
Why is my test not working? Or where in the code have I done something wrong?
Don’t extend HashMap as you’re doing. Use a HashMap and delegate to it:
At the moment, Repository is a HashMap, but you don’t store anything in it: you store the values in another HashMap contained in Repository.
Also, storing an iterator in a field is a bad idea. iterators can be used only once. Once they have iterated, the can’t iterate anymore. It should be a local variable.