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
Subhodeep Dey 1Subhodeep Dey 1 

Issue in test class (Code coverage) for PDF report

Class code:-

public class ReportClass {
    public Campaign CampignDetails{get;set;}
    public List<CampaignMember> campaignMemberRecords {get; set;}
    public List<Opportunity> opportunityLists {get; set;}
    public set<String> campaignMemberStatus {get; set;}
    public List<wrapClass> wrpperValues {get; set;}
    public Map<Id, Boolean> OpportunityPresentOrNot {get; set;}
    public Map<id, Set<String>> opportunityStageCampaignMemberIdMap {get; set;} //Camp id with opportunity Stage
    public Map<Id, Map<string, integer>> opportunityTotalNumWithStage {get; set;}
    public Map<Id, Map<string, Decimal>> opportunityTotalAmountWithStage {get; set;}
    public Map<Id, Decimal> campaignMemberWithopportunityTotalAmount {get; set;}
    public ReportClass(){
        campaignMemberRecords = new List<CampaignMember>();
        opportunityLists = new List<Opportunity>();
        campaignMemberStatus = new set<String>();
        opportunityStageCampaignMemberIdMap = new Map<id, Set<String>>(); //Got
        wrpperValues = new List<wrapClass>();
        OpportunityPresentOrNot = new Map<Id, Boolean>();
        Set<id> campaignMembersId = new Set<id>();
        opportunityTotalNumWithStage = new Map<Id, Map<string, integer>>();
        opportunityTotalAmountWithStage = new Map<Id, Map<string, Decimal>>();
        campaignMemberWithopportunityTotalAmount = new Map<Id, Decimal>();
        
        /*-----------------------*/
        String cmpId;
        if(test.isRunningTest()){
           Campaign cp = new Campaign();
             cp.Name = 'Test Account' ;
            cp.IsActive = TRUE;
            insert cp;
            cmpId = cp.id;
        }
        else if(!test.isRunningTest()){
           cmpId = ApexPages.currentPage().getParameters().get('id');
        }
        
        /*----------------------*/
        CampignDetails = [SELECT Id,name,Number_of_Sent__c,Number_of_Responded__c,NumberOfContacts,
                          NumberOfOpportunities,NumberOfWonOpportunities,AmountAllOpportunities,AmountWonOpportunities
                       //   from Campaign WHERE ID=:ApexPages.currentPage().getParameters().get('id')];
                            from Campaign WHERE ID=:cmpId];
        /*----------------------*/
        
        List<Opportunity> allOpportunity = new List<Opportunity>();
        List<Opportunity> temporaryOpportunityList = new List<Opportunity>();
        Map<id, List<opportunity>> champAndopportunityMap = new Map<id, List<opportunity>>();
        //To fetch CampaignMember
        for(CampaignMember cmpMem:[SELECT name,CampaignId,ContactId,Campaign_Active__c,Campaign_Name__c,CompanyOrAccount,Email,FirstName,
                                   HasResponded,LastName,Member_City__c,Member_State__c,Member_Street__c,Member_Zip__c,
                                   Phone,Status FROM CampaignMember WHERE CampaignId =:ApexPages.currentPage().getParameters().get('id') ]){
            campaignMemberRecords.add(cmpMem);
            campaignMembersId.add(cmpMem.ContactId);
            campaignMemberStatus.add(cmpMem.Status); // To get campaignMember Status
        }
        
        
        //To fetch Opportunity
        for(Opportunity op : [SELECT AccountId,Account.Name,Campaign.Name,npsp__Primary_Contact__r.Name,
                              Amount,CampaignId,CloseDate,Name,npsp__Primary_Contact__c,RecordTypeId,RecordType.Name, 
                              StageName FROM Opportunity WHERE npsp__Primary_Contact__c IN: campaignMembersId AND
                              CampaignId = :ApexPages.currentPage().getParameters().get('id')]){
        allOpportunity.add(op);
        }
       
       // To fetch campaignMembers Id with List<opporutnity> and Opportunity Stage
        for(id ids : campaignMembersId){
             temporaryOpportunityList = new List<opportunity>();
            for(opportunity op : allOpportunity){
                if(op.npsp__Primary_Contact__c == ids){
                    temporaryOpportunityList.add(op);
                }
            }
            if(temporaryOpportunityList.size() > 0){
                OpportunityPresentOrNot.put(ids, True);
            }
            else{
                OpportunityPresentOrNot.put(ids, False);
            }
            Set<String> OpportunityStageSet = new Set<String>();
            for(opportunity op : temporaryOpportunityList){
                OpportunityStageSet.add(op.StageName);
            }
            opportunityStageCampaignMemberIdMap.put(ids,OpportunityStageSet);
            champAndopportunityMap.put(ids, temporaryOpportunityList);
        }
        
        //To fetch Total amount and Total no of records per stage
        for(id ids : campaignMembersId){
            List<Opportunity> TempOppList = new List<Opportunity>();
            Map<string, integer> TempOppStageWithTotalNo = new Map<string, integer>();
            Map<string, Decimal> TempOppStageWithTotalAmount = new Map<string, Decimal >();
            for(string oppStage : opportunityStageCampaignMemberIdMap.get(ids)){
                TempOppList = new List<Opportunity>();
                for(Opportunity opp :champAndopportunityMap.get(ids)){
                    Decimal totalAmount = 0;
                    if(opp.StageName == oppStage){
                        TempOppList.add(opp);
                    }
                    if(TempOppList.size() > 0 ){
                    for(Opportunity oppTemp : TempOppList){
                        totalAmount += oppTemp.Amount;
                    }
                    }
                    else{
                        totalAmount = 0;
                    }
                    TempOppStageWithTotalAmount.put(oppStage, totalAmount);
                    TempOppStageWithTotalNo.put(oppStage, TempOppList.size());
                }
                
            }
            opportunityTotalNumWithStage.put(ids, TempOppStageWithTotalNo);
            opportunityTotalAmountWithStage.put(ids, TempOppStageWithTotalAmount);
        }
        system.debug(opportunityTotalAmountWithStage);
        
        for(id ids : campaignMembersId){
            Decimal totalOpportunityAmount = 0;
            for(Opportunity opp :champAndopportunityMap.get(ids)){
                totalOpportunityAmount += opp.Amount;
            }
            campaignMemberWithopportunityTotalAmount.put(ids, totalOpportunityAmount);
        }
        
        //Put value in WRAPPER CLASS
        for(campaignMember campaignMemberRec : campaignMemberRecords){
            wrpperValues.add(new wrapClass(campaignMemberRec, champAndopportunityMap.get(campaignMemberRec.ContactId), champAndopportunityMap.get(campaignMemberRec.ContactId).size(), campaignMemberWithopportunityTotalAmount.get(campaignMemberRec.ContactId), campaignMemberStatus, opportunityStageCampaignMemberIdMap.get(campaignMemberRec.ContactId),OpportunityPresentOrNot.get(campaignMemberRec.ContactId), opportunityTotalNumWithStage.get(campaignMemberRec.ContactId), opportunityTotalAmountWithStage.get(campaignMemberRec.ContactId)));
        }
        
    }

    //Wrapper Class
    public class wrapClass{
        public CampaignMember campaignMemberRecordForWrap {get; set;} //got
        public List<Opportunity> opportunityListsForWrap {get; set;}  //got
        public Integer totalNoOfOpportunityWrap {get; set;}
        public Decimal totalAmountOfOpportunityWrap {get; set;}
        public Set<String> campaignMemberStatusForWrap {get; set;} //got
        public Set<String> OpportunityStagesForWrap {get; set;} //got
        public boolean OpportunityPresentOrNotForWrap {get; set;}
        public Map<string, integer> opportunityTotalNumWithStageForWrap {get; set;}
        public Map<string, Decimal> amountWithOpportunityStagesForWrap {get; set;}
        //public Map<String, Integer> amountWithOpportunityStagesForWrap {get; set;}   , Map<String, Integer> amountWithOpportunityStages
        public wrapClass(CampaignMember campaignMemberRecord, List<Opportunity> opportunityLists, Integer totalNoOfopportunity, Decimal totalAmountOfOpportunity, Set<String> campaignMemberStatus, Set<String> OpportunityStages, Boolean OpportunityPresentOrNot, Map<string, integer> opportunityTotalNumWithStage, Map<string, Decimal> opportunityTotalAmountWithStage){
            campaignMemberRecordForWrap = campaignMemberRecord;
            opportunityListsForWrap = opportunityLists;
            totalNoOfOpportunityWrap = totalNoOfopportunity;
            totalAmountOfOpportunityWrap = totalAmountOfOpportunity;
            campaignMemberStatusForWrap = campaignMemberStatus;
            OpportunityStagesForWrap = OpportunityStages;
            OpportunityPresentOrNotForWrap = OpportunityPresentOrNot;
            opportunityTotalNumWithStageForWrap = opportunityTotalNumWithStage;
            amountWithOpportunityStagesForWrap = opportunityTotalAmountWithStage;
        }
    }
}

Current test class code with pass and 60% code coverage

@isTest(SeeAllData = true)
public class ReportClass_Test {

    static testMethod void TestCampaignMember (){
        Test.startTest();
        Account testAcc = new Account (Name = 'Test Account');{
        insert testAcc;
        }
        Id cmpRecordTypeId = Schema.SObjectType.Campaign.getRecordTypeInfosByName().get('Marketing Campaign').getRecordTypeId();
        
        //Creates Contact to be linked to Campaign Member
        Contact testContact = new Contact(FirstName = 'TestContactF', LastName = 'TestContactL', Email = 'none@gmail.com',accountid=testAcc.Id);
        insert testContact;
        
        Campaign cp= [SELECT Id,name,Number_of_Sent__c,Number_of_Responded__c,NumberOfContacts,
                          NumberOfOpportunities,NumberOfWonOpportunities,AmountAllOpportunities,AmountWonOpportunities
                          from Campaign limit 1];
    
        //Creates a new campaign memeber, associaites it with 1 provider sales campaign, and inserts
        CampaignMember newMember = new CampaignMember(ContactId = testContact.id, status='Sent', campaignid = cp.id);
        insert newMember;
     
         Opportunity testOpp = new Opportunity (Name = 'Test Name',                       
                                     AccountId = testAcc.Id,
                                     StageName = 'Asked',
                                     Amount = 50000.00,
                                     CloseDate = System.today(),
                                     campaignid = cp.id,
                                     npsp__Primary_Contact__c =testContact.id
                                     );
        insert testOpp;
     
        Opportunity testOpp1 = new Opportunity (Name = 'Test Name1',                       
                                     AccountId = testAcc.Id,
                                     StageName = 'Received',
                                     Amount = 10000.00,
                                     CloseDate = System.today() + 1,
                                     campaignid = cp.id,
                                     npsp__Primary_Contact__c =testContact.id
                                     );
        insert testOpp1; 
    
        List<Opportunity> opList = new List<Opportunity>();
        opList.add(testOpp);
        opList.add(testOpp1); 
        
        Set<String> memStatus =new Set<String> ();
        memStatus.add(newMember.Status);
        
        Set<String> opStage =new Set<String> ();
        opStage.add(testOpp.StageName); 
        opStage.add(testOpp1.StageName); 
        
        Map<string, integer> opportunityTotalNumWithStage = new  Map<string, integer>();
        opportunityTotalNumWithStage.put(testOpp.StageName,1);
        Map<string, Decimal> opportunityTotalAmountWithStage = new Map<string, Decimal>();
        opportunityTotalAmountWithStage.put(testOpp.StageName, 50000.00);
    
         
        ReportClass rc= new ReportClass();
        rc.campaignMemberRecords.add(newMember);
        ReportClass.wrapClass rcWrp = new  ReportClass.wrapClass(newMember,opList,1,50000.00,memStatus,opStage,true,opportunityTotalNumWithStage,opportunityTotalAmountWithStage);
              
        Test.stopTest();

    }
}
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Subhodeep,

May I suggest you please refer the "Test Generator app" you can create test classes for Apex Controller, Class or Trigger. Hope it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar