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
sarika12sarika12 

Need help on code coverage issue

I have created a test class for one of my controller but after so many trial it is still showing me 73. please suggest .

 
Maulik D ShahMaulik D Shah

Hello S G 35,

Can you please share your test class and controller to check?

sarika12sarika12
Hi Maulik,

it is 1700+ lines of code, unable to share it. Hence oly sharing the part where it is showing red lines for controller
String urlPart = 'parentOppId=' + newMasterOpp.Id;
        if (newChildOpps.size() > 0)
        {
            if (newMasterOpp.Id!=null) {
                for (Opportunity o: newChildOpps)
                {
                    o.ParentId__c = newMasterOpp.Id;
                }
            }
            
            try
            {
                insert newChildOpps;
            }
            catch (DMLException e)
            {
                pushError('There was an error creating the child opportunites in the renewal package: ' + e.getMessage());
                
                Database.rollback(sp);
                return null;
            }

           if (newMasterOpp.Id==null && newChildOpps.size() > 1) {
                Id parentId = newChildOpps[0].Id;
                Decimal paCount = 0;
                for (Opportunity co : [SELECT Id, Num_of_Agreement_Sites__c FROM Opportunity WHERE Id IN : newChildOpps]) {
                    if (co.Num_of_Agreement_Sites__c!=null && co.Num_of_Agreement_Sites__c > paCount) {
                        parentId = co.Id;
                        paCount = co.Num_of_Agreement_Sites__c;
                    }
                }
                for (Opportunity co : newChildOpps) {
                    if (co.Id != parentId) {
                        co.ParentId__c =  parentId;
                    }
                }

                update newChildOpps;

                List<Agreement_Site__c> agList = new List<Agreement_Site__c>();

                for (Agreement_Site__c ag : [SELECT Id FROM Agreement_Site__c WHERE Opportunity__c IN : newChildOpps AND Opportunity__c != :parentId]) {
                    ag.Master_Opportunity__c = parentId;
                    agList.add(ag);
                }

                update agList;

                urlPart = 'parentOppId=' + parentId;
            }
            else if (newMasterOpp.Id==null && newChildOpps.size()==1) {
                urlPart = 'Id=' + newChildOpps[0].Id;
            }            
        }

please suggest how to get coverage for these lines of code
sarika12sarika12
It is one of the method of test class.
private static TestMethod void TestOneOpportunityPageController2() {
        
         System.assertEquals('COM', OneOpportunityPageController.parseZone('test (COM)'));
        System.assertEquals('', OneOpportunityPageController.parseZone('test'));
        System.assertEquals('', OneOpportunityPageController.parseZone('test (COM'));
        System.assertEquals('', OneOpportunityPageController.parseZone('test COM)'));
        System.assertEquals(null, OneOpportunityPageController.parseZone(null));
        
        Utility_Product_Junction__c junction = new Utility_Product_Junction__c();
        Agreement_Site__c paForLookup = new Agreement_Site__c();
        String ProductId = '';
        String UtilityId = '';
        Agreement_Site__c productAgreement = new Agreement_Site__c();
        Account account = new Account();
        Opportunity opp = new Opportunity();

        try {
            junction = [
                SELECT  Id,EnerNOC_Product__c, Utility_Account__c 
                FROM    Utility_Product_Junction__c 
                WHERE   EnerNOC_Product__r.Type__c = 'DemandSMART' 
                LIMIT 1
            ];

            productAgreement = [
                SELECT  Id,Opportunity__c,Opportunity__r.AccountId, Opportunity__r.StageName 
                FROM    Agreement_Site__c 
                WHERE   Opportunity__c != NULL  and
                        Opportunity__r.StageName like '1%' and
                        Opportunity__r.ParentID__c = ''
                LIMIT   1
            ];

            UtilityId = junction.Utility_Account__c;
            ProductId = junction.EnerNOC_Product__c;
        } catch (QueryException e) {
            system.debug('error getting DR junctions: ' + e.getMessage());
        }

        if (productAgreement != null && junction != null) {
            account = new Account(
                Id = productAgreement.Opportunity__r.AccountId,
                Utility_Account__c = UtilityId
            );

            opp = new Opportunity(
                Id = productAgreement.Opportunity__c, 
                Product_Lookup__c = ProductId,
                termination_date__c = System.Date.today().addYears(2),
                effective_date__c = System.Date.today().addYears(-1),
                estimated_margin_start_date__c = System.Date.today().addYears(-1),
                estimated_margin_end_date__c = System.Date.today().addYears(2)
            );

            productAgreement.Account__c = productAgreement.Opportunity__r.AccountId;
            productAgreement.Product__c = ProductId;
            productAgreement.StageName__c = productAgreement.Opportunity__r.StageName;
            productAgreement.List_Price__c = 1000;
            productAgreement.Monthly_Solution_Discount__c = 25;

            List<Apttus__APTS_Agreement__c> agreements = new List<Apttus__APTS_Agreement__c>();
            Apttus__APTS_Agreement__c agreement2 = new Apttus__APTS_Agreement__c(Apttus__Related_Opportunity__c = opp.Id,
                                                                                 Apttus__Auto_Renew_Consent__c = true,
                                                                                 Apttus__Contract_End_Date__c = System.Date.today().addYears(2),
                                                                                 Apttus__Status_Category__c = 'In Effect',
                                                                                 ENOC_Renewal_Terms_List__c = 'Successive two-year terms.');
            agreements.add(agreement2);
            Apttus__APTS_Agreement__c agreement3 = new Apttus__APTS_Agreement__c(Apttus__Related_Opportunity__c = opp.Id,
                                                                                 Apttus__Auto_Renew_Consent__c = true,
                                                                                 Apttus__Contract_End_Date__c = System.Date.today().addYears(2),
                                                                                 Apttus__Status_Category__c = 'In Effect',
                                                                                 ENOC_Renewal_Terms_List__c = 'Successive three-year terms.');
            agreements.add(agreement3);
            Apttus__APTS_Agreement__c agreement4 = new Apttus__APTS_Agreement__c(Apttus__Related_Opportunity__c = opp.Id,
                                                                                 Apttus__Auto_Renew_Consent__c = true,
                                                                                 Apttus__Contract_End_Date__c = System.Date.today().addYears(2),
                                                                                 Apttus__Status_Category__c = 'In Effect',
                                                                                 ENOC_Renewal_Terms_List__c = 'Successive four-year terms.');
            agreements.add(agreement4);
            Apttus__APTS_Agreement__c agreement5 = new Apttus__APTS_Agreement__c(Apttus__Related_Opportunity__c = opp.Id,
                                                                                 Apttus__Auto_Renew_Consent__c = true,
                                                                                 Apttus__Contract_End_Date__c = System.Date.today().addYears(2),
                                                                                 Apttus__Status_Category__c = 'In Effect',
                                                                                 ENOC_Renewal_Terms_List__c = 'Successive five-year terms.');
            agreements.add(agreement5);

            try {
                update account;
                update opp;
                update productAgreement;
                insert agreements;
            } catch (DmlException e) {
                system.debug('error updating records: ' + e.getMessage());
            }

            
            PageReference OneOppPage = Page.OneOpportunityPage;
            Test.setCurrentPageReference(OneOppPage);
            ApexPages.CurrentPage().getParameters().put('Id',opp.Id);
            ApexPages.CurrentPage().getParameters().put('workingAccountId',account.Id);

            OneOpportunityPageController controller = new OneOpportunityPageController();
            controller.pageLoadActions();
            controller.getProductTableFields();
            controller.setEditProductId(productAgreement.Id);
            controller.getEnernocProductList();
            controller.setThisenernocProduct(ProductId);
            controller.EditProduct();           
            controller.save();
            controller.RemoveProduct();
            controller.CancelEdit();
            controller.getReadyForAgmt();
            controller.GenerateAgreement();
            controller.ViewProduct();
            controller.CreateRenewalPackage();
            controller.getEditProduct();
            //controller.pushError('test');

            ApexPages.currentPage().getParameters().put('ProductSelect', ProductId);
                
            controller.ChangeProduct();

            controller.editProductAgreement.StageName__c = '7-Lost';
            
            ApexPages.currentPage().getParameters().put('oppStageName', controller.editProductAgreement.StageName__c);
                
            controller.ChangeStage();
            
            List<Account> accounts = controller.getAccounts();
            List<Schema.FieldSetMember> fieldSetMembers = controller.getAccountDetailFields();
            Map<Id,List<Agreement_Site__c>> productAgreementsMap = controller.getProductAgreements();
            String editProductId = controller.getEditProductId();
            List<SelectOption> responseTypeList = controller.getResponseTypeList();
            String productType = controller.getThisProductType();
            String enernocProduct = controller.getThisEnernocProduct();
            Boolean viewProduct = controller.getViewProduct();
            Boolean masterIsWon = controller.getMasterIsWon();
            Boolean oppIsRenewed = controller.getOppIsRenewed();
            controller.editProductAgreement.Monthly_Solution_Discount__c = 25;
            controller.editProductAgreement.List_Price__c = 1000;
            Decimal monthlyServiceFee = controller.getMonthlyServiceFee();
            Id accountId = controller.getAccountId();
                
            Test.startTest();
            controller.save();
            
            PageReference pr = controller.NewProduct();
            pr = controller.NewProductAll();            
            pr = controller.OpenParentOpp();
            pr = controller.OpenOpportunity();
            pr = controller.OpenProduct();
            pr = controller.calculateMonthlySolutionFee();
            pr = controller.refresh();
            Test.stopTest();
        }
    }