Reputation: 1304
I have the below method which I want to sub:
ProductArray productsArray;
productsArray = dataService.getProducts(ProductFilter pf, Date date1, Date date2, boolean matured);
In my test class I have below
ProductArray productsArrayTest = getProductsArrayForTest();
Mockito.when(dataServiceMock.getProducts(Mockito.any(ProductFilter.class), Mockito.any(Date.class), Mockito.any(Date.class), Mockito.any(Boolean.class))).thenReturn(productsArrayTest);
However in this test productsArray
returns as null
;
Mockito gives the below warning
[MockitoHint] 1. Unused... -> at
com.company.util.TestProduct.testProductLoad(TestProduct.java:157)
[MockitoHint] ...args ok? -> at
com.company.datastore.ProductLoader.processAll(ProductLoaderProcess.java:158)
What could be the reason for this?
Edit:
private ProductArray getProductsArrayTest() {
ProductArray pa = new ProductArray();
Product product = createProduct();
pa.add(product);
return pa;
}
private Product createProduct() {
Product p = new Product();
p.setPrice(1.23);
return p;
}
Upvotes: 0
Views: 651
Reputation: 963
Edit after the first comment - I see that you call the method getProductsArrayForTest() while you define getProductsArrayTest(). Check if it is only a mistake in the post or even in the code. However here is the code and the test succeeds.
public class Product {
double price;
public Product() {
}
public double getPrice() {
return this.price;
}
public void setPrice(double value) {
this.price=value;
}
}
ProductArray.class
import java.util.ArrayList;
import java.util.List;
public class ProductArray {
private List<Product> productList;
public ProductArray() {
productList=new ArrayList<Product>();
}
public void add(Product product) {
this.productList.add(product);
}
public List<Product> getProductList(){
return this.productList;
}
}
ProductTest.class
public class ProductTest {
@Test
public void testProduct() {
DataService dataserviceMock = Mockito.mock(DataService.class);
ProductArray productsArrayTest = getProductsArrayTest();
Mockito.when(dataserviceMock.getProducts(Mockito.any(ProductFilter.class), Mockito.any(Date.class), Mockito.any(Date.class), Mockito.anyBoolean())).thenReturn(productsArrayTest);
assertEquals(1, dataserviceMock.getProducts(null , null , null , false).getProductList().size());
}
private ProductArray getProductsArrayTest() {
ProductArray pa = new ProductArray();
Product product = createProduct();
pa.add(product);
return pa;
}
private Product createProduct() {
Product p = new Product();
p.setPrice(1.23);
return p;
}
}
Upvotes: 1