salesforcecode-coverageapexapex-codelwc

Apex test class for the for loop is not been covered what needs to be done


public class ItemTypeWrapper {
    @AuraEnabled
    public Id ItemTypeId ;
    @AuraEnabled
    public String ItemType;
    public ItemTypeWrapper(Id ItemTypeId, String ItemType) {
        this.ItemTypeId = ItemTypeId;`enter code here`
        this.ItemType = ItemType;
    }
} 
//method to fetch details of the Item type from the sObject POS_Item__c
@AuraEnabled(cacheable=true)
public static List<ItemTypeWrapper> getItemTypeList_Apex()
{
    //variable to hold the account details
    objAccount=getAccount();
    //to fetch the profile details of the logged in user
    String profileName=[Select Id,Name from Profile where Id=:userinfo.getProfileId()].Name;
    //instance is created itemTypeResults of type List<POS_Item__c >
    List<POS_Item__c> itemTypeResults = new List<POS_Item__c>();
    //to hold the unique Item type ids 
    Set<Id> uniqueItemTypeIds = new Set<Id>();
    //check if the profileName is salesmanager
    if(profileName == folioSalesManagerProfile)
    {
       //fetch the item type details for salesmanager profile where marketing only is false
       itemTypeResults = [SELECT Id, Type_of_Item__r.Item_Type__c,Type_of_Item__c FROM POS_Item__c WHERE 
                                   Account__c=:objAccount.Id AND Active__c=true AND Item_ID__c!=null AND (Inventory_Seasonal_Program__c='Inventory' OR Inventory_Seasonal_Program__c='Both') AND Marketing_Only__c=false
                                   AND Logical_Invenory_Stock__c > 0 ORDER BY Type_of_Item__r.Item_Type__c];
    }
    else
    {
        itemTypeResults = [SELECT Id, Type_of_Item__r.Item_Type__c,Type_of_Item__c FROM POS_Item__c WHERE 
                                   Account__c=:objAccount.Id AND Active__c=true AND Item_ID__c!=null AND (Inventory_Seasonal_Program__c='Inventory' OR Inventory_Seasonal_Program__c='Both')
                                   AND Logical_Invenory_Stock__c > 0 ORDER BY Type_of_Item__r.Item_Type__c];
    }
    //instance is created itemTypeWrappers of type List<ItemTypeWrapper>
    List<ItemTypeWrapper> itemTypeWrappers = new List<ItemTypeWrapper>();
    //loop through the itemTypeResultsForAFacility , for each
    for (POS_Item__c result : itemTypeResults ) {
        //get the Item type record id and store it in a container itemTypeId
        Id itemTypeId = (Id)result.get('Type_of_Item__c');
        // Check if itemTypeId does not contains in the container uniqueItemTypeIds , if yes
        if (!uniqueItemTypeIds.contains(itemTypeId)) {
            //add the ItemType to the container ItemType
            String ItemType = result.Type_of_Item__r.Item_Type__c;
            //Create a new ItemTypeWrapper instance and add it to the list.
            itemTypeWrappers.add(new ItemTypeWrapper(itemTypeId, ItemType));
            //Add 'itemTypeId' to 'uniqueItemTypeIds' 
            uniqueItemTypeIds.add(itemTypeId);
        }
    }

    //return the itemTypeWrappers
    System.debug('itemTypeWrappers----'+itemTypeWrappers);
    return itemTypeWrappers;
}    

this is my test class

@isTest public class testClass {

@isTest
private static void addToCartFunctionalityTestMethod ()
{
    Account testAccount=[SELECT Id FROM Account LIMIT 1];
    Brand__c testBrand=[SELECT Id FROM Brand__c LIMIT 1];
    Item_Type__c testItemtype=[SELECT Id FROM Item_Type__c LIMIT 1];
    List<POS_Item__c> testPosItems=[SELECT Id FROM POS_Item__c];
    
    Test.startTest();
    ApexClass.getBrandList_Apex();
    ApexClass.getItemTypeList_Apex();
    Test.stopTest();
}
@testSetup static void setupData() {
    List<Account> testAccts = new List<Account>();
    Account acct = new Account(Name='demo');
    insert acct ;
    Contact contact=new Contact(LastName='Testing',AccountId=acct.Id);
    insert contact;
    Profile profile=[SELECT Name FROM Profile WHERE Name='Customer Community Plus User' LIMIT 1];
    
    User communityUser = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                                  EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                                  LocaleSidKey='en_US', ProfileId = profile.Id, 
                                  TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com',ContactId=contact.Id);
    
    insert communityUser;
    
    Brand__c brand = new Brand__c (Brand_Name__c='BrandNameSelect',Active__c=true,Account__c=acct.Id);
    insert brand;
    
    Item_Type__c itemType= new Item_Type__c(Item_Type__c='itemTypeSelect',Active__c=true,Account__c=acct.Id);
    insert itemType;
    POS_Item__c posItem= new POS_Item__c (Item_No__c='Test-No-01',Item_Name__c='testPosItemName',Available_Stock__c=10,
                                          Account__c=acct.Id,Brand__c=brand.Id,Type_of_Item__c=itemType.Id,Active__c=true);
    insert posItem;
    
    POS_Item__c posItem2= new POS_Item__c (Item_No__c='Test-No-02',Item_Name__c='testPosItemName',Available_Stock__c=10,
                                           Account__c=acct.Id,Brand__c=brand.Id,Type_of_Item__c=itemType.Id,Active__c=true);
    insert posItem2;
}

}

The test class contains all the dummy data require for the sobject but still the for loop for the unique Item type is not been covered please help me with the test class coverage

I tried with fake test class coverage but for now i am not expecting to put fake coverage

Thanks in advance


Solution

  • This happens due to incorrect test data. When setting up test data you are creating this POS_Item__c object with some values but when retrieving that same object in you actual method you are looking for data that is not there. Specifically (Inventory_Seasonal_Program__c='Inventory' OR Inventory_Seasonal_Program__c='Both') condition which is not true on your test data.

    So when setting up test data, it should look like this:

    POS_Item__c posItem= new POS_Item__c (
        Item_No__c = 'Test-No-01',
        Item_Name__c = 'testPosItemName',
        Available_Stock__c = 10,
        Account__c = acct.Id,
        Brand__c = brand.Id,
        Type_of_Item__c = itemType.Id,
        Active__c = true,
        Inventory_Seasonal_Program__c = 'Inventory'
    );
        
    insert posItem;