0
        public class WCT_CountryrealignUpdate2{
        @InvocableMethod
        public static void NewcountyUpdate2(List<id> countyIds1){
        Boolean t;
            List<New_country__c> newval = new List<New_country__c>();
            for (AggregateResult ar : [select count(Id) totalCount, Company_Realign__c countyId From Contact where Company_Realign__c = :countyIds1 GROUP BY Company_Realign__c ] ){
                Decimal getCount = (Decimal) ar.get('totalCount');
                  t=getCount>0?true:false;
                newval.add(new New_country__c(
                    Id = (Id) ar.get('countyId'), 
                    Alumni_contact_count__c = (Decimal) ar.get('totalCount'),Alumni_Present__c = t
                ));
                    System.debug('22222222222222'+ar);

            }
            update newval;
        }
        }

Test class:

@isTest
public class WCT_CountryrealignUpdateTest
{
public static  testmethod void count()
{
   List<New_country__c> CountryList = New List<New_country__c>();

   New_country__c company = new New_country__c(Name='Test098765', Domestic_Ultimate_DUNS_Number__c='012345678',Alumni_Present__c = True,Alumni_contact_count__c = 5,Parent__c = False,Status__c = 'Approved');
    insert company;

     New_country__c company1 = new New_country__c(Name='Test098765', Domestic_Ultimate_DUNS_Number__c='012345678',Alumni_Present__c = True,Alumni_contact_count__c = 5,Parent__c = False,Status__c = 'Approved');
    insert company1;

     User u = WCT_UtilTestDataCreation.createUser('test77642@cvm.com','System Administrator','CTS Out of Scope - Vendor Relationship Management','CVMLast','CVMFirst','cvmcvm',);  
    insert u;

    recordtype rt=[select id from recordtype where Name = 'Employee'];
    Contact cnt1 =WCT_UtilTestDataCreation.createEmployee(rt.id);
    cnt1.Company_Realign__c = company.id;
    insert cnt1;
    cnt1.Company_Realign__c = company1.id;
    update cnt1;
     List<id> Cid = New List<id>();
      Cid.add(cnt1.id);


    Test.startTest();

   WCT_CountryrealignUpdate.NewcountyUpdate(Cid);

    Test.stopTest();

  }    
}

enter image description here

Not able cover the higlited lines of code. Can any one please help to how cover for that piece of code.

6

You are passing the wrong parameter to your method NewcountyUpdate(). It is coded to accept a set of Id values for New_country__c objects, and you are passing it a set containing the Id of one Contact. As a result, your aggregate query is guaranteed to return no results, as it's querying based on the supplied Ids corresponding to New_country__c records.

Your Set should contain the Ids of company and company1 for your test to execute as designed.

Additionally, your unit test should be querying the updated data in your New_country__c records after executing NewcountyUpdate() and making assertions about the results. If you do not do so, your unit test becomes a "smoke test", which proves nothing except that the code does not crash under a specific set of circumstances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.