I am testing an enterprise level application using Mockito and JUnit. Here is the code for a method of adding a product to the offline repository class in a product offline-repository-class-test I have:
@Mock
private InitialData initialData;
@InjectMocks
private ProductRepositoryOffline pro;
@Test
public void testPersistProduct() {
Product product = new Product(0, "", "", "", 0.0, true, "", 0, /*Product type*/null, "", 0, 0);
ArrayList<Product> productList = new ArrayList<Product>();
//productList.add(product);
Mockito.when(initialData.getProducts()).thenReturn(productList);
pro.persistProduct(product);
assertEquals(pro.getProducts().get(0), product);
}
This relies on the following methods in classes:
The method it is testing in the ProductRepositoryOffline:
@Override
public void persistProduct(Product pr) {
initialData.addProduct(pr);
}
InitialData
private ArrayList<Product> products = new ArrayList<Product>();
public void addProduct(Product product) {
products.add(product);
}
The question I wish to ask is that in the case of pro.persistProduct(product) unless I have product already added to the ArrayList, isn't persistProduct meant to be adding product to the arrayList without the need for the commented productList.add(product)?
initialDatadoes in the methodgetProducts, which I assumeProductRepositoryOfflinecalls in it's version ofgetProducts(). What shouldinitialData.addProduct(pr)do? At the moment, it does nothingproductList.add(product);, the item ofproductwould never be added to the list.