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
RICARDO PALMARICARDO PALMA 

Test coverage 0% in a Controller Extension

Hi, 
I'm having a hard time covering this Controller Extension,  any example code?
Thanks!!!

public with sharing class currentbilling {
public final Account acc {get; set;}
public integer countActRec {get; set;}
public  Object sumAmount{set;get;}
Set<ID> countr = new Set<ID>();  
   public currentbilling(ApexPages.StandardController controller) {
       this.acc = (Account)controller.getRecord();
   }
   public List<OpportunityLineItem> oppoproRecords{get;set;} 
public void FetchData() {
  oppoproRecords = [SELECT Id, Opportunityid, Opportunity.Name ,  PricebookEntry.Product2.Name , Discounted_Rate__c, Advertiser_Account__c,  Opportunity.Contract_End_Date__c  FROM OpportunityLineItem Where Advertiser_Account__c = :acc.id and  Opportunity.StageName  = 'Contract Executed' and (Cancellation_Date__c = null or Cancellation_Date__c > Today) ];
if(oppoproRecords.isEmpty()){
  ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'There is no executed opportunities with actives products for this account.'));
}
  for (OpportunityLineItem rec : oppoproRecords) {
   countr.add(rec.Opportunityid);
   }
  countActRec =   [SELECT count() From Opportunity Where id in :countr];
  AggregateResult[] groupedResults   = [SELECT SUM(Discounted_Rate__c)aver FROM OpportunityLineItem Where Advertiser_Account__c = :acc.id and  Opportunity.StageName  = 'Contract Executed' and (Cancellation_Date__c = null or Cancellation_Date__c > Today)];
  sumAmount = groupedResults[0].get('aver');
}
}
------------
Best Answer chosen by RICARDO PALMA
Manohar kumarManohar kumar

Hi Ricardo,

Please try below code. Some fields are missing but this should work.

 

@isTest(seeAllData=true)

Public class Test{

    public static testMethod void test1() {
        account acc = new account(name='a');
        insert acc;
        
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

        Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk1;

        Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
        insert prd1;


        PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
        insert pbe1;


        Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Contract Executed',CloseDate=Date.today(),Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id);
        insert opp1;


        OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id, quantity=4, totalprice=200);
        insert lineItem1;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        currentbilling cb = new currentbilling (sc);
        cb.FetchData();
    
    }


Thanks,

Manohar

All Answers

Manohar kumarManohar kumar

Hi Ricardo,

Please try below code. Some fields are missing but this should work.

 

@isTest(seeAllData=true)

Public class Test{

    public static testMethod void test1() {
        account acc = new account(name='a');
        insert acc;
        
        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

        Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
        insert pbk1;

        Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
        insert prd1;


        PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
        insert pbe1;


        Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Contract Executed',CloseDate=Date.today(),Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id);
        insert opp1;


        OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id, quantity=4, totalprice=200);
        insert lineItem1;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        currentbilling cb = new currentbilling (sc);
        cb.FetchData();
    
    }


Thanks,

Manohar

This was selected as the best answer
RICARDO PALMARICARDO PALMA
Thanks Manohar.