I need to test a function in junittestcase my function is:
public static List<string> getProductIdsFromStringValues(String productCollectionValue) {
List<string> productsIds = new ArrayList<>();
String[] bundleProducts = productCollectionValue.split(Constants.COMMA_DELIMITER_REGEX);
for (String bundleProduct : bundleProducts) {
String[] bundleProductWithCount = bundleProduct.split(Constants.COUNT_SEPERATOR);
if (bundleProductWithCount.length > 0) {
productsIds.add(bundleProductWithCount[0]);
}
}
return productsIds;
}
What I have tried:
my testcase is:
public void testgetProductIdsFromStringValues() throws Exception {
String productCollectionValue = new String();
List<string> productsIds = CatalogUtil.getProductIdsFromStringValues(productCollectionValue);
Assert.assertTrue("List not empty", productsIds.isEmpty());
}
but it shows assertion error. I'm beginner in java and junit.anyhelp would be appreciated
Edit: Added from comment:
Assert.assertTrue("List not empty", productsIds.isEmpty());
this line shows error. because
productIds
returns
""
. so when I check
ProductsIds.Empty
it shows assertion error. but I need to pass
productCollectionValue
as a empty string, so only it fails in this if condition
if (bundleProductWithCount.length > 0) {
productsIds.add(bundleProductWithCount[0]);
}