function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Thanigai Kumaran BalajiThanigai Kumaran Balaji 

Unable to cover more than 45% of test coverage, test doesn't cover for loop even after condition is met

my apex class:
@RestResource(urlMapping = '/v1/UpdateFWAReferralCode/*')
global class FWAReferralCode {

    @HttpPost
    global static Void Updatecode(Integer updatelimit) {

        RestResponse res = RestContext.response;
        if (res == null) {
            res = new RestResponse();
            RestContext.response = res;
        }
        List < Account > accid = [SELECT Id, (Select id, name from assets where name = : label.BasePlanName or name = : label.PlusPlanName) FROM Account WHERE Network__c = 'CORE'
        AND vlocity_cmt__Status__c = 'Active'
        AND Customer_Group__c != 'Free Trial'
        AND ID IN(Select Accountid from asset where name = : label.BasePlanName or name = : label.PlusPlanName) AND Id NOT IN(SELECT Account__c FROM FWA_Referral_Code__c WHERE Account__c != null) LIMIT: updatelimit];
        //List<FWA_Referral_Code__c> FWAcode = [select ID,Plan__c,Mapped_Timestamp__c,Account__c from FWA_Referral_Code__c where Account__c = NULL limit :updatelimit];
        if (accid.size() > 0) {
            system.debug(accid.size());
        }
        List < FWA_Referral_Code__c > FWAupdate = new List < FWA_Referral_Code__c > ();
        List < ID > Responselist = new List < ID > ();
        List < FWA_Referral_Code__c > FWAResponse = new List < FWA_Referral_Code__c > ();
        try {
            for (Account ac: accid) {
                FWA_Referral_Code__c fwa = [select ID, Plan__c, Mapped_Timestamp__c, Account__c from FWA_Referral_Code__c where Account__c = NULL limit 1];
                Responselist.add(fwa.id);
                fwa.account__c = ac.id;
                fwa.Mapped_Timestamp__c = DateTime.Now();
                if (ac.assets[0].name == label.BasePlanName) {
                    fwa.Plan__c = label.BasePlanName;
                    system.debug(fwa.Plan__c);
                } else if (ac.assets[0].name == label.PlusPlanName) {
                    fwa.Plan__c = label.PlusPlanName;
                    system.debug(fwa.Plan__c);
                }
                FWAupdate.add(fwa);

                update FWAupdate;
                system.debug(FWAupdate);
            }
            for (FWA_Referral_Code__c resp: [select ID, Account__c from FWA_Referral_Code__c where Id = : Responselist]) {
                FWAResponse.add(resp);
            }
            if (FWAResponse.size() > 0) {
                res.responseBody = Blob.valueOf(Json.serialize(FWAResponse));
                system.debug(res.responseBody);
                res.addHeader(Label.label_Content_Type, Label.label_application_json);
                res.statusCode = 200;
                system.debug(res.statusCode);
                return;
            }

        } catch (Exception e) {
            System.debug('Error: ' + e.getMessage());
        }
    }
}




Test class:
@isTest
Public class FWAReferralCodeTest {

    Public static testMethod void FWAReferralTest() {

        FWA_Referral_Code__c fwa = new FWA_Referral_Code__c(name = 'testing');
        Account ac = new Account(Name = 'Thanigai testfwa', vlocity_cmt__Status__c = 'Active', Network__c = 'CORE', ServiceSharingType__c = 'Group', Customer_Group__c = 'Test');
        Asset ass = new asset(name = 'visible+ plan', Accountid = ac.Id);
        insert fwa;
        insert ass;
        insert ac;
        FWA_Referral_Code__c fwa1 = new FWA_Referral_Code__c(name = 'testing1');
        Account ac1 = new Account(Name = 'Thanigai fwa', vlocity_cmt__Status__c = 'Active', Network__c = 'CORE', ServiceSharingType__c = 'Group', Customer_Group__c = 'Test');
        Asset ass1 = new asset(name = 'visible plan', Accountid = ac.Id);
        insert ass1;
        insert fwa1;
        insert ac1;
        FWA_Referral_Code__c fw = new FWA_Referral_Code__c(Account__c = ac1.id, Mapped_Timestamp__c = DateTime.Now(), Plan__c = label.BasePlanName);
        FWA_Referral_Code__c fw1 = new FWA_Referral_Code__c(Account__c = ac.id, Mapped_Timestamp__c = DateTime.Now(), Plan__c = label.PlusPlanName);
        insert fw;
        insert fw1;
        Test.startTest();
        FWAReferralCode.Updatecode(1);

        Test.stopTest();

    }
}
SwethaSwetha (Salesforce Developers) 
HI Balaji,
Your question does not highlight which lines are code are uncovered.
Since this code is huge and requires an understanding of your implementation, it might not be possible to provide exact edit suggestions. However, the below articles give a good insight into how coverage can be improved
 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 

 Code Coverage Best Practices
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_code_coverage_best_pract.htm
 
Checking Code Coverage
https://help.salesforce.com/articleView?id=code_dev_console_tests_coverage.htm&type=5

Example test classes:
https://salesforce.stackexchange.com/questions/185163/a-testclass-for-restresource-httppost
https://salesforce.stackexchange.com/questions/320673/create-a-test-class-for-restresource-class-with-a-response-code

Thanks