I have a class which adds features to categories of products in the same category and prices based on the productCode of the product. I have a List of List of products in the same category and productCode for which the same price and category should be added. However, when I create a mock for the ProductDecorator it fails to return any products. I am using Lombok's @EqualsAndHashCode.Include on productCode in both classes
Here is the code
public class ProductDetail {
String productCode;
String category;
Double price;
public String getProductCode() {
return productCode;
}
}
class Product {
String name;
String productCode;
String category;
Double price;
String getProductCode() {
return productCode;
}
public Product addDetail(String category, Double price) {
this.category = category;
this.price = price;
return this;
}
}
public class ProductDecorator {
List<Product> addProductFeature(List<Product> products, List<ProductDetail> details) {
List<Product> productList = new ArrayList<>();
products.forEach(product -> {
details.forEach(detail -> {
if (product.getProductCode().equals(detail.getProductCode())) {
Product p = product.addDetail(detail.category, detail.price);
productList.add(p);
}
});
});
return productList;
}
}
// 3 Products and 3 product details objects that match
List<List<Product>> products = getProducts()
List<ProductDetail> productDetailList = getProductDetails()
ProductDecorator productDecorator = Mock()
// Returns null all the time for all, just showing the first one
productDecorator.addProductFeature(products[0],productDetailList)>>[products[0]]
How do I successfully mock the decorator to match. I think it is not matching because of some equality issues.I tried using underscores but that did not work either. I'd be grateful for a solution to thsi problem
For me, it works like this, so probably your original code looks different:
Please note: The mock should return
products[0]rather than[products[0]], becauseProductDecorator.addProductFeaturereturnsList<Product>, notList<List<Product>>. But that is not the root cause of your problem.Try it in the Groovy Web Console.
There, I used the Groovy equivalent of your Lombok annotations for simplicity's sake.
@Canonicalis like a combination of@ToString,@EqualsAndHashCodeand@TupleConstructor. While recreating your situation, I was also printing values, hence the wish to have nicetoString()methods. I also converted your lambdas into Groovy closures. Lambdas are fine in Groovy 4, but closures also work in older Groovy versions.