Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to test a function in junittestcase my function is:
Java
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:
Java
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:
Java
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
Java
if (bundleProductWithCount.length > 0) {
    productsIds.add(bundleProductWithCount[0]);
}
Posted
Updated 9-Nov-20 22:46pm
v2
Comments
Richard MacCutchan 10-Nov-20 3:42am    
"but it shows assertion error."
What error, where?
deepika44 10-Nov-20 4:05am    
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 condtion
if (bundleProductWithCount.length > 0) {
productsIds.add(bundleProductWithCount[0]);
}
Richard MacCutchan 10-Nov-20 4:25am    
Well that is correct. If you send a blank string to that method then it will not return any products.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900