+ Start a Discussion
Andreea TAndreea T 

Need to cover 3 % on test class

Hi there,
Please help me to cover only 3 %, my test class covering 72% only, Attaching the screenshot with the red part which needs to cover.
Please guys help me.
Thanks
User-added imageUser-added imageUser-added image
mukesh guptamukesh gupta
Hi Andreea, 

Please send you test class code

Regards
Mukesh
Andreea TAndreea T
Hi Mukesh
Here is my test class, please help me with it
@isTest 
public with sharing class FarmingCampaignCreatorControllerTest {
    public static testmethod void testController(){
        
        List <Lead> Leads = new List <Lead> ();
        for(integer i=0; i<20; ++i) {
            Leads.add(testDataFactory.createLead('Lead'+i));           
        }
        insert Leads;
        List <Account> toInsert = new List <Account> ();
        
        Campaign C = new Campaign(Name='Farming España', Country__C ='Spain');
        insert C;
        
        Test.startTest();
        
        for(integer i=0; i<20; ++i) {
            Account A = TestDataFactory.createAccount(i.format());
            toInsert.add(A);
        }
        toInsert[0].RecordTypeId = Util.getAccountRTId('Approved_account');
        toInsert[0].Account_Status__c = 'Preapproved';
        
        insert toInsert;
        
        Account accOne = TestDataFactory.createAccount('A');
        accOne.Account_Status__c = 'Approved';
        accOne.Type_of_Organisation__c = 'Charity';
        accOne.Total_Amount_Invoiced__c = 100.00;
        accOne.Annual_FX__c = 200;
        accOne.ParentId = toInsert[0].Id;
        accOne.Type_of_Organisation__c ='Individual';
        accOne.Account_Status__c = 'PreApproved';
        insert accOne;
        accOne.Account_Status__c = 'Suspended';
        update accOne;

        String fType ='Farming Type';
        String fTypeOne = 'SaReGaMaPa';
  
        Contact cntOne = new Contact();
        cntOne.LastName = 'Test contact';
        cntOne.Email = 'test@test.com';
        cntOne.FeedbackOnCSMSent__c = true;
        insert cntOne;
        
        Profile pOne = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        User uOne = new User(
            Alias = 'standt', 
            Email='test234@test.com',
            EmailEncodingKey='UTF-8', 
            LastName='Testing', 
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US', 
            ProfileId = pOne.Id,
            TimeZoneSidKey='America/Los_Angeles', 
            UserName='standarduser1@test.com'
        );
        insert uOne;
        
        Transaction__c To = TestDataFactory.createOrder(200,accOne);
        To.Normalized_Amount__c = 20.00;
        To.Client__c = accOne.Id;
        To.All_in__c = True;
        To.Signing_Date__c = Date.today().addDays(-5);
        To.Status__c = 'In Progress';

        Transaction__c Tt = TestDataFactory.createTrade(200,accOne);
        Transaction__c Tc = TestDataFactory.createClearing(200,accOne);
        
        List <Transaction__c> T = new List <Transaction__c> {To,Tt,Tc};
        insert T;
        
        Group grpOne = new Group();
        grpOne.Name = 'Technology Partner Content';
        grpOne.DeveloperName = 'TechnologyPartnerContent';
        insert grpOne;
        grpOne.DeveloperName = 'FarmingSpainOne';
        update grpOne;
            
            
        FarmingCampaignCreatorController FCDC= new FarmingCampaignCreatorController();            
        FCDC.farmingMethod='Churn';
        FCDC.farmingMethod='Follow Up';
        FCDC.farmingMethod='Cash Collection';
        FCDC.farmingMethod='Partial FX';
        FCDC.doFindCandidates();
        FCDC.doCreateCampaign();
        FCDC.doRemove();
        FCDC.getActivation();
        FCDC.getCashCollection();
        FCDC.getPartialFX();
        FCDC.refreshData();
        FCDC.getChurn();
        FarmingCampaignCreatorController.getFollowUp();
        
        Account accOneAgain = new Account();
        accOneAgain.Name = 'testOne';
        insert accOneAgain;
        
        String farmingMethod ='Activation';
        PageReference pageRef = Page.FarmingCampaignCreatorView;
        Test.setCurrentPage(pageRef);
        
        String auxCamName = System.today().year()+'/'+System.today().month()+'/'+System.today().day()+' - Farming Campaign - ' + farmingMethod;
        
        User testUser = TestDataFactory.createUser('TestUser', 'System Administrator');
        insert testUser;
        List<Contract> cons = new List<Contract>();
        List<Account> accs = new List<Account>();
        Account testAcc = TestDataFactory.createApprovedAccount('Test1');
        testAcc.BillingCountry = 'Spain';
        testAcc.CC_Invoices__c = 'dummy@dummy.dummy';
        
        accs.add(testAcc);
        
        insert accs;
        
        cons.add(new ContractBuilder().setAccountID(accs[0].Id)
                 					.setContractTerm(1)
                 					.setMonthlySWSubscription(100)
                					.setStartDate(System.today())
                					.setSubscriptionPackage('FX Service')
                					.setBillingAgreement('All Upfront')
                					.build());
        
        insert cons;
        
        for(contract con:cons){
            con.Status = 'Activated';
        }
        Map<Id,Contract> contracts = New Map<Id,Contract> (Cons);
        update cons;
        
        
        List<Subscription_Monthly_Revenue__c> smrs = new List<Subscription_Monthly_Revenue__c>();
        for (Subscription_Monthly_Revenue__c smr : [SELECT id, invoice_reviewed__c from Subscription_Monthly_Revenue__c]){
            smr.invoice_reviewed__c = true;
            smr.invoiced__c = true;
            smrs.add(smr);
        }
        update smrs;
        
        Invoice__c newInv = new Invoice__c(
            
            Account__c = testAcc.id,
            Subscription_Invoice_Number__c =null,
            Contract__c = smrs[0].contract__r.id,
            Subscription_Monthly_Revenue__c = smrs[0].id,
            Amount__c = 1000,
            Currency__c = (smrs[0].contract__r.Fixed_Exchange_Currency__c == null?'EUR':smrs[0].contract__r.Fixed_Exchange_Currency__c),
            Normalized_Amount__c = smrs[0].invoiced_revenue__c,
            Normalized_Currency__c = 'EUR',
            Type__c = 'OrderInvoice',
            Date_Invoice__c = system.today(),
            Status__c = 'invoice_accepted',
            Invoice_Description__c = smrs[0].contract__r.Subscription_package__c,
            VAT_ID__c = smrs[0].contract__r.subscription_holder__c == null? smrs[0].contract__r.account.VAT_ID__c:smrs[0].contract__r.subscription_holder__r.VAT_ID__c,
            VAT_Type__c = smrs[0].contract__r.subscription_holder__c == null? smrs[0].contract__r.account.VAT_Type__c:smrs[0].contract__r.subscription_holder__r.VAT_Type__c,
            OwnerID = testUser.id,
            recordTypeID = Schema.SObjectType.Invoice__c.getRecordTypeInfosByName().get('OrderInvoice').getRecordTypeId()
        );
        
        insert newInv;

        Campaign c1 = new Campaign();
        c1.StartDate = System.today().addDays(-1); 
        c1.EndDate = System.today().addDays(1);
        c1.Name='ParentCampaign';
        
        Campaign c2 = new Campaign();
        c2.StartDate = System.today().addDays(-1);
        c2.EndDate = System.today().addDays(1); 
        c2.Name='ChildCampaign'; 
        c2.ParentId = c1.Id;
        c2.isActive = True;
        c2.Status = 'In Progress';
        c2.Country__c ='Other';
        c2.OtherCountry__c ='Global';
        c2.Type ='Farming';
        
        Campaign c3 = new Campaign();
        c3.StartDate = System.today().addDays(-1); 
        c3.EndDate = System.today().addDays(1); 
        c3.Name='ParentCampaign'; 
        c3.ParentId = c1.Id;
        c3.isActive = True;
        c3.Status = 'Planned';
        c3.Country__c ='Other';
        c3.OtherCountry__c ='UK';
        c3.Type ='Fair';

        insert new List<Campaign>{c1,c2,c3};
        
        CampaignMember cm = new CampaignMember();
        
		AccountCampaignMember__c acm = new AccountCampaignMember__c();
		CampaignMemberComponentController.CampaignMemberWrapper cmw = new CampaignMemberComponentController.CampaignMemberWrapper(cm);
		CampaignMemberComponentController.ACMWrapper acmw = new CampaignMemberComponentController.ACMWrapper(acm);
        
        FarmingCampaignCreatorController.candidate fcOne = new FarmingCampaignCreatorController.Candidate(accOne,fType);
        
        fcOne.flag = True;
        fcOne.member = accOne;
        fcOne.farmingType ='Farming Type';
        fcOne.tradedIn = 100.00;
        fcOne.turnoverGap =fcOne.member.Annual_FX__c - (fcOne.tradedIn*12/6);
        
        FarmingCampaignCreatorController.candidate  fwcOne = new FarmingCampaignCreatorController.candidate(accOneAgain,fTypeOne);
        
        fwcOne.flag = true;
        fwcOne.member = accOneAgain;
        fwcOne.farmingType ='Number One Farming Type';
        fwcOne.tradedIn = 50.00;
        fwcOne.turnoverGap =30.00;
        
        Test.stopTest();
    }
        public static testmethod void test2Controller(){
        
        List <Lead> Leads = new List <Lead> ();
        for(integer i=0; i<20; ++i) {
            Leads.add(testDataFactory.createLead('Lead'+i));           
        }
        insert Leads;
        List <Account> toInsert = new List <Account> ();
        
        Campaign C = new Campaign(Name='Farming España', Country__C ='Spain');
        insert C;
        
        Test.startTest();
        
        for(integer i=0; i<20; ++i) {
            Account A = TestDataFactory.createAccount(i.format());
            toInsert.add(A);
        }
        toInsert[0].RecordTypeId = Util.getAccountRTId('Approved_account');
        toInsert[0].Account_Status__c = 'Preapproved';
        
        insert toInsert;
        
        Account accOne = TestDataFactory.createAccount('A');
        accOne.Account_Status__c = 'Approved';
        accOne.Type_of_Organisation__c = 'Charity';
        accOne.Total_Amount_Invoiced__c = 100.00;
        accOne.Annual_FX__c = 200;
        accOne.ParentId = toInsert[0].Id;
        accOne.Type_of_Organisation__c ='Individual';

        accOne.Account_Status__c = 'PreApproved';
        insert accOne;
        accOne.Account_Status__c = 'Suspended';
        update accOne;

        String fType ='Farming Type';
        String fTypeOne = 'SaReGaMaPa';
  
        Contact cntOne = new Contact();
        cntOne.LastName = 'Test contact';
        cntOne.Email = 'test@test.com';
        cntOne.FeedbackOnCSMSent__c = true;
        insert cntOne;
        
        Profile pOne = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        User uOne = new User(
            Alias = 'standt', 
            Email='test234@test.com',
            EmailEncodingKey='UTF-8', 
            LastName='Testing', 
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US', 
            ProfileId = pOne.Id,
            TimeZoneSidKey='America/Los_Angeles', 
            UserName='standarduser1@test.com'
        );
        insert uOne;
        
        Transaction__c To = TestDataFactory.createOrder(200,accOne);
        To.Normalized_Amount__c = 20.00;
        To.Client__c = accOne.Id;
        To.All_in__c = True;
        To.Signing_Date__c = Date.today().addDays(-5);
        To.Status__c = 'In Progress';

        Transaction__c Tt = TestDataFactory.createTrade(200,accOne);
        Transaction__c Tc = TestDataFactory.createClearing(200,accOne);
        
        List <Transaction__c> T = new List <Transaction__c> {To,Tt,Tc};
        insert T;
        
        Group grpOne = new Group();
        grpOne.Name = 'Technology Partner Content';
        grpOne.DeveloperName = 'TechnologyPartnerContent';
        insert grpOne;
        grpOne.DeveloperName = 'FarmingSpainOne';
        update grpOne;
            
            
        FarmingCampaignCreatorController FCDC= new FarmingCampaignCreatorController();            
        FCDC.farmingMethod='Churn';
        FCDC.farmingMethod='Follow Up';
        FCDC.farmingMethod='Cash Collection';
        FCDC.farmingMethod='Partial FX';
        FCDC.doFindCandidates();
        FCDC.doCreateCampaign();
        FCDC.doRemove();
        FCDC.getActivation();
        FCDC.getCashCollection();
        FCDC.getPartialFX();
        FCDC.refreshData();
        FCDC.getChurn();
        FarmingCampaignCreatorController.getFollowUp();
        
        
        Account accOneAgain = new Account();
        accOneAgain.Name = 'testOne';
        insert accOneAgain;
        
        String farmingMethod ='Activation';
        PageReference pageRef = Page.FarmingCampaignCreatorView;
        Test.setCurrentPage(pageRef);
        
        String auxCamName = System.today().year()+'/'+System.today().month()+'/'+System.today().day()+' - Farming Campaign - ' + farmingMethod;
        
        User testUser = TestDataFactory.createUser('TestUser', 'System Administrator');
        insert testUser;
        List<Contract> cons = new List<Contract>();
        List<Account> accs = new List<Account>();
        Account testAcc = TestDataFactory.createApprovedAccount('Test1');
        testAcc.BillingCountry = 'Spain';
        testAcc.CC_Invoices__c = 'dummy@dummy.dummy';
        
        accs.add(testAcc);
        
        insert accs;
        
        cons.add(new ContractBuilder().setAccountID(accs[0].Id)
                 					.setContractTerm(1)
                 					.setMonthlySWSubscription(100)
                					.setStartDate(System.today())
                					.setSubscriptionPackage('FX Service')
                					.setBillingAgreement('All Upfront')
                					.build());
        
        insert cons;
        
        for(contract con:cons){
            con.Status = 'Activated';
        }
        Map<Id,Contract> contracts = New Map<Id,Contract> (Cons);
        update cons;
        
        
        List<Subscription_Monthly_Revenue__c> smrs = new List<Subscription_Monthly_Revenue__c>();
        for (Subscription_Monthly_Revenue__c smr : [SELECT id, invoice_reviewed__c from Subscription_Monthly_Revenue__c]){
            smr.invoice_reviewed__c = true;
            smr.invoiced__c = true;
            smrs.add(smr);
        }
        update smrs;
        
        Invoice__c newInv = new Invoice__c(
            
            Account__c = testAcc.id,
            Subscription_Invoice_Number__c =null,
            Contract__c = smrs[0].contract__r.id,
            Subscription_Monthly_Revenue__c = smrs[0].id,
            Amount__c = 1000,
            Currency__c = (smrs[0].contract__r.Fixed_Exchange_Currency__c == null?'EUR':smrs[0].contract__r.Fixed_Exchange_Currency__c),
            Normalized_Amount__c = smrs[0].invoiced_revenue__c,
            Normalized_Currency__c = 'EUR',
            Type__c = 'OrderInvoice',
            Date_Invoice__c = system.today(),
            Status__c = 'invoice_accepted',
            Invoice_Description__c = smrs[0].contract__r.Subscription_package__c,
            VAT_ID__c = smrs[0].contract__r.subscription_holder__c == null? smrs[0].contract__r.account.VAT_ID__c:smrs[0].contract__r.subscription_holder__r.VAT_ID__c,
            VAT_Type__c = smrs[0].contract__r.subscription_holder__c == null? smrs[0].contract__r.account.VAT_Type__c:smrs[0].contract__r.subscription_holder__r.VAT_Type__c,
            OwnerID = testUser.id,
            recordTypeID = Schema.SObjectType.Invoice__c.getRecordTypeInfosByName().get('OrderInvoice').getRecordTypeId()
        );
        
        insert newInv;

        Campaign c1 = new Campaign();
        c1.StartDate = System.today().addDays(-1); 
        c1.EndDate = System.today().addDays(1);
        c1.Name='ParentCampaign';
        
        Campaign c2 = new Campaign();
        c2.StartDate = System.today().addDays(-1);
        c2.EndDate = System.today().addDays(1); 
        c2.Name='ChildCampaign'; 
        c2.ParentId = c1.Id;
        c2.isActive = True;
        c2.Status = 'In Progress';
        c2.Country__c ='Other';
        c2.OtherCountry__c ='Global';
        c2.Type ='Farming';
        
        Campaign c3 = new Campaign();
        c3.StartDate = System.today().addDays(-1); 
        c3.EndDate = System.today().addDays(1); 
        c3.Name='ParentCampaign'; 
        c3.ParentId = c1.Id;
        c3.isActive = True;
        c3.Status = 'Planned';
        c3.Country__c ='Other';
        c3.OtherCountry__c ='UK';
        c3.Type ='Fair';

        insert new List<Campaign>{c1,c2,c3};
        
        CampaignMember cm = new CampaignMember();
        
		AccountCampaignMember__c acm = new AccountCampaignMember__c();
		CampaignMemberComponentController.CampaignMemberWrapper cmw = new CampaignMemberComponentController.CampaignMemberWrapper(cm);
		CampaignMemberComponentController.ACMWrapper acmw = new CampaignMemberComponentController.ACMWrapper(acm);
        
        FarmingCampaignCreatorController.candidate fcOne = new FarmingCampaignCreatorController.Candidate(accOne,fType);
        
        fcOne.flag = True;
        fcOne.member = accOne;
        fcOne.farmingType ='Farming Type';
        fcOne.tradedIn = 100.00;
        fcOne.turnoverGap =fcOne.member.Annual_FX__c - (fcOne.tradedIn*12/6);
        
        FarmingCampaignCreatorController.candidate  fwcOne = new FarmingCampaignCreatorController.candidate(accOneAgain,fTypeOne);
        
        fwcOne.flag = true;
        fwcOne.member = accOneAgain;
        fwcOne.farmingType ='Number One Farming Type';
        fwcOne.tradedIn = 50.00;
        fwcOne.turnoverGap =30.00;
        
        Test.stopTest();
    }
}
It might not be good as I am new to coding.
Thanks a lot!!!
SUCHARITA MONDALSUCHARITA MONDAL

Hi Andreea,

In your test class, keep your Campign Name and auxCamName same, so that SOQL can fetch the record in you Apex Class (Line no 84 as per screenshot). Since your are using Non-Deterministic values (i.e. System.Today() etc.). which I guess not matching.

Hope this helps!

Thanks
Sucharita