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
Anna Antonella Adiletta 1Anna Antonella Adiletta 1 

write test class for the apex code

I'm trying to write test class, for an apex code.  I do not understand  why the code coverage is yet  60% . Can someone help me? thank you

public class ModuloIncaricoInteraziendaleCtrl {
        
    private Quote quote {get; set;}
    public List<Qlip> qlips {get; set;}
 
    public class QliP {
        
        public QuoteLineItem qli {get; set;}
        public List<Partecipazione__c> partecipazioni {get; set;}
        public integer numeropartecipazioni {get; set;}
        
        public QliP(QuoteLineItem qlix, List<Partecipazione__c> partecipazionix){
            this.qli = qlix;
            this.partecipazioni = partecipazionix;
            this.numeropartecipazioni = partecipazionix.size();
        }
        
    }
 
    public ModuloIncaricoInteraziendaleCtrl(ApexPages.StandardController Ctrl){
        
        if (!Test.isRunningTest()) {Ctrl.addfields(new list<string>{'QuoteLineItems', 'Account.Name'});}
        this.quote=(Quote)Ctrl.getRecord();
        
        List<Id> QuoteLineItemsId = new List<Id>();
        for (QuoteLineItem qli : quote.QuoteLineItems){
            QuoteLineItemsId.add(qli.Id);
        }
        
        List<QuoteLineItem> queryQLI = [SELECT Id, LineNumber, Product2.Name, Edizione__r.Name, Edizione__r.Data_inizio__c, Edizione__r.Data_fine__c, Edizione__r.Ha_Partecipazioni__c, TotalPrice, Importo_IVA__c, Totale__c FROM QuoteLineItem WHERE Id IN :QuoteLineItemsId];
        List<Partecipazione__c> queryPartecipazioni = [SELECT Id, Name, Voce_Preventivo__c, Referente__r.FirstName, Referente__r.LastName, Referente__r.MiddleName, Referente__r.Suffix, Referente__r.Posizione__c, Referente__r.Email FROM Partecipazione__c WHERE Voce_Preventivo__c IN :QuoteLineItemsId ORDER BY Voce_Preventivo__c];
        
        qlips = new List<QliP>();
        for (QuoteLineItem qli : queryQLI) {
            
            List<Partecipazione__c> suePart = new List<Partecipazione__c>();
            integer NpartIgnote = 0;
            for (Partecipazione__c p : queryPartecipazioni){
                if(p.Voce_Preventivo__c == qli.Id){
                    if(p.Referente__c == NULL){
                        NpartIgnote++;
                        Contact cont = new Contact(LastName = 'Nominativo da comunicare ' + NpartIgnote + ' ' + Quote.Account.Name);
                        p.Referente__r = cont;
                        system.debug(p.Referente__r.Name);
                    }
                    suePart.add(p);
                }
            }
            Qlip newQlip = new QliP(qli, suePart);
            qlips.add(newQlip);
        }
        
    }

}


@istest
private class ModuloIncaricoInteraziendaleCtrl_Test {
    
    private static TestMethod void TestCostruttore(){
       
        List<Account> Lacc = new List <Account>();
        Account acc= new Account (FirstName='Antonio', LastName='Verdi', MiddleName='pippo',Type='Analyst' );
        insert acc;
        Lacc.add(acc);
        
        Edizione__c ed = new Edizione__c ( Name='test', Data_inizio__c=date.valueOf('2016-09-07'), Data_fine__c=date.valueOf('2016-09-07'),Ha_partecipazioni__c=true);
        insert ed;
       
        List <Contact> Lctc= new List <Contact>();
        Contact ctc= new Contact (LastName='Magneti', FirstName='Roberto', MiddleName='mauro', Suffix='Junior',email='k@email.it', posizione__c='direttore commerciale');
        insert ctc; 
        Lctc.add(ctc);
   
        List<Opportunity> l_opp = new List<Opportunity>();
        Opportunity opp = new Opportunity( AccountId=acc.id, Name='test', StageName='Prospecting',CloseDate=date.today(),Type='New Client',NextStep = 'Test',LeadSource = 'Business Development');
        insert opp;
        l_opp.add(opp);

     Product2 p2 = new Product2 (name='test',IsActive=true);
     List <Product2> Lp2 = new List <Product2>();
     insert p2;
     Lp2.add(p2);
     
     Id pricebookId = Test.getStandardPricebookId();

     List <PricebookEntry> Lpbe = new List <PricebookEntry>();
     PricebookEntry pbe = new PricebookEntry (Product2Id=p2.Id,IsActive=true,UnitPrice=1,Pricebook2Id=pricebookId);
     insert pbe;
     Lpbe.add(pbe);

List <Quote> Lqt = new List <Quote> ();
     Quote preventivo = new Quote(Name = 'Test',OpportunityId = opp.id, N_Offerta__c = 'ciao', Pricebook2Id=pricebookId, ordine__c=false);
     insert preventivo;
        Lqt.add(preventivo);
        
 List<QuoteLineItem> Lqtl = new List<QuoteLineItem> ();
   QuoteLineItem qtl = new quotelineitem(quoteid = preventivo.id, quantity=1, unitprice=1, PricebookEntryId=pbe.id, Product2Id=p2.id, Edizione__c=ed.id );
   insert qtl;
    Lqtl.add(qtl);
      
     Partecipazione__c pt = New Partecipazione__c(stato__c='partecipante',Voce_preventivo__c=qtl.id, referente__c=ctc.id);
    insert pt;   
   List <Partecipazione__c> Lpt= new List <partecipazione__c> ();
     Lpt.add(pt);
        
         Partecipazione__c pt2 = New Partecipazione__c(stato__c='partecipante',Voce_preventivo__c=qtl.id, referente__c=ctc.id);
    insert pt2; 
     
       pt.Voce_Preventivo__c = qtl.Id;
        pt.Referente__c = null;
        pt.referente__c= contact;
         update ctc;
        
        system.debug(pt.Referente__r.Name);
        
        pt2.Voce_Preventivo__c = qtl.Id;
        pt2.Referente__c = null;
        pt2.referente__c= contact;
        update ctc;
        
        system.debug(pt2.Referente__r.Name);
        
     
        
    ApexPages.StandardController Controller = new ApexPages.StandardController(preventivo);
        
    ModuloIncaricoInteraziendaleCtrl stdController = new  ModuloIncaricoInteraziendaleCtrl(controller);
        
    ModuloIncaricoInteraziendaleCtrl.QliP prova = new ModuloIncaricoInteraziendaleCtrl.QliP(qtl,Lpt);
    
    } 
}      
        
   

 
Biz ZaveriBiz Zaveri
Can you attach the Log file that's generated when running the test case?
Anna Antonella Adiletta 1Anna Antonella Adiletta 1
This is the raw log
38.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
09:34:52.0 (357055)|EXECUTION_STARTED
09:34:52.0 (407519)|CODE_UNIT_STARTED|[EXTERNAL]|VisualForce View State
09:34:52.0 (10839134)|CODE_UNIT_FINISHED|VisualForce View State
09:34:52.0 (12004244)|EXECUTION_FINISHED
09:34:52.584 (584603856)|CUMULATIVE_LIMIT_USAGE
09:34:52.584 (584603856)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

09:34:52.584 (584603856)|CUMULATIVE_LIMIT_USAGE_END






 
Anna Antonella Adiletta 1Anna Antonella Adiletta 1
User-added image
Biz ZaveriBiz Zaveri
The Quote object does not have a way of getting to the Quote Line Items. You may have to change your code to do the following:

List<QuoteLineItem> queryQLI = [SELECT Id, LineNumber, Product2.Name, Edizione__r.Name, Edizione__r.Data_inizio__c, Edizione__r.Data_fine__c, Edizione__r.Ha_Partecipazioni__c, TotalPrice, Importo_IVA__c, Totale__c FROM QuoteLineItem WHERE QuoteId = :this.quote.Id];

You can also get rid of: 
 List<Id> QuoteLineItemsId = new List<Id>();
for (QuoteLineItem qli : quote.QuoteLineItems){
     QuoteLineItemsId.add(qli.Id);
}