I have a Class named Item.
public class Item {
private int code;
private String name;
private double price;
private int quantity;
public Item(int code, String name, double price, int quantity) {
setCode(code);
setName(name);
setPrice(price);
setQuantity(quantity);
}
//Item getters and setters
And I have a Class named Stock that creates an ArrayList of Items.
public class Stock {
private ArrayList<Item> stock;
public Stock() {
stock = new ArrayList<Item>();
}
public ArrayList<Item> getStock() {
return stock;
}
I also have a Class named ItemRegister that adds an Item to the Stock.
public class ItemRegister extends Stock {
public void registerItem(String name, double price) {
getStock().add(new Item(setItemCode(), name, price, 0));
}
private int setItemCode() {
return getStock().size() + 1;
}
I'm using unit tests to see if I did add an Item to the Stock.
public class ItemRegisterTest {
@Test
public void testIfHasRegisteredItemInStock() {
Stock s = new Stock();
assertTrue(s.getStock().size() == 0);
ItemRegister i = new ItemRegister();
i.registerItem("Oleo", 20.0);
assertTrue(s.getStock().size() == 1);
}
}
When I run these tests it is returning an error.
On the second assertTrue, if I test with the object i it'll return true but what I want is to add to Stock and not ItemRegister because if later on I wanna consult Stock I'll call Stock and not ItemRegister.
sobject is in any way related to youriobject? Why or why not?assertTrue(i.getStock().size() == 1);sbyilike this assertTrue(i.getStock().size() == 1);shas zero elements.