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
Paolo Magliocco 18Paolo Magliocco 18 

Test Class. Help me

I 'm writing this class with its test class and I can save my code because there aren't errors but test class don't cover code of the class.
Can we help me to solve problems? 
Thank you.

This is Class : 

public class Ordine_Aziendale {


   public Quote preventivo{get; set;}
   public List<Account> account{get; set;}
   public Opportunity opportunita{get; set;}
   public List<QuoteLineItem> qtl{get; set;}
   public Integer cont{get;set;}
   
   public User utente{get; set;}  
    
    public Ordine_Aziendale(ApexPages.StandardController stdcontroller) {
    
      this.preventivo = (Quote)stdController.getRecord();
    
   // Product2 p2 = new Product2 (name='test');
      //  insert p2;
        
        qtl = [SELECT id , product2.id, product2.name, product2.ProductCode, Quantity, UnitPrice FROM QuoteLineItem  WHERE Quote.id =: preventivo.id];
        cont = qtl.size();
        
      // account = [SELECT id,Name,BillingAddress,ShippingAddress FROM Account WHERE id =: preventivo.id];
     // opportunita = [SELECT id,Name,BillingAddress,ShippingAddress FROM Account WHERE id =: preventivo.AccountId];
    }
}


This is my test class :

@isTest
public class Ordine_Aziendale_Test {
    static testMethod void Costruttore_Test()
    {
     
 /************************* START TEST   *************************************/      
 
     test.startTest();
      
     List<Account> l_acc = new List<Account>();
     Account acc = new Account(name='Test Account', billingStreet='Test', billingCity='Test', billingState='Test', billingCountry='Test');
     insert acc;
     l_acc.add(acc);
  
     List<Opportunity> l_opp = new List<Opportunity>();
     Opportunity opp = new Opportunity();
     opp.Accountid = acc.id;
     opp.Name = 'test';
     opp.StageName = 'Prospecting';
     opp.CloseDate = date.today();
     opp.Type = 'New Client';
     opp.NextStep = 'Test';
     opp.LeadSource = 'Business Development';
     insert opp;
     l_opp.add(opp);
      
     List<Quote> l_preventivi = new List<Quote>();  
     Quote preventivo = new Quote(Name = 'Test',OpportunityId = opp.id, N_Offerta__c = 'test');
     insert preventivo;
     l_preventivi.add(preventivo);
     
     // Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
        
     Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
     insert pb;
        
      Product2 prod = new Product2(Name = 'Anti-infectives 2007', Family = 'Best Practices', IsActive = true);
      insert prod;

     List<QuoteLineItem> l_qtl = new List<QuoteLineItem>();
     QuoteLineItem qtl = new quotelineitem(quoteid = preventivo.id,quantity = 1,unitprice = 1); 
     insert qtl;
     l_qtl.add(qtl);

      ApexPages.StandardController controller = new ApexPages.StandardController(preventivo);
      Ordine_Aziendale stdController = new Ordine_Aziendale(controller);



/********************************************** FINISH TEST   *************************************/      
 
   test.stopTest();

   }
  
 }

 
parth jollyparth jolly
Hi Paolo ,


Just add "N_Offerta__c = 'test' " in my code rest all works like a charm 
Code covergae - 100%
Please let me if error arises .


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
@isTest
public class Ordine_Aziendale_Test {
    static testMethod void Costruttore_Test()
    {
     
 /************************* START TEST   *************************************/      
 
     test.startTest();
      
     List<Account> l_acc = new List<Account>();
     Account acc = new Account(name='Test Account', billingStreet='Test', billingCity='Test', billingState='Test', billingCountry='Test');
     insert acc;
     l_acc.add(acc);
  
     List<Opportunity> l_opp = new List<Opportunity>();
     Opportunity opp = new Opportunity();
     opp.Accountid = acc.id;
     opp.Name = 'test';
     opp.StageName = 'Prospecting';
     opp.CloseDate = date.today();
     opp.Type = 'New Client';
     opp.NextStep = 'Test';
     opp.LeadSource = 'Business Development';
     insert opp;
     l_opp.add(opp);
      
     List<Quote> l_preventivi = new List<Quote>();  
     Quote preventivo = new Quote(Name = 'Test',OpportunityId = opp.id);
     insert preventivo;
     l_preventivi.add(preventivo);
     
     Product2 p1 = new Product2();
        p1.Name='Test8';
        p1.Productcode='PO16';
        p1.IsActive = true;        
        insert p1;      

        Id standardPB =  Test.getStandardPricebookId();
        
        PricebookEntry pbe = new PricebookEntry();
        pbe.IsActive = true;
        pbe.Product2ID = p1.Id;
        pbe.Pricebook2Id = standardPB;
        pbe.UnitPrice = 200;        
        //pbe.UseStandardPrice = false;
        insert pbe;
                
        Quote q=new Quote();
        q.Name='Test';        
        q.OpportunityId=opp.Id;
        q.Pricebook2Id=standardPB;
        insert q;
       
        QuoteLineitem q1 = new QuoteLineItem(
        PricebookEntryId = pbe.Id,
        Quantity = 1,
        UnitPrice = pbe.UnitPrice,
        QuoteId = q.Id
        );
        
        insert q1;
        
     List<QuoteLineItem> l_qtl = new List<QuoteLineItem>();
    
     l_qtl.add(q1);
     
   Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];


    User u = new User(
      Alias = 'testu',
      Email = 'test@unitingambition.com',
      EmailEncodingKey = 'UTF-8',
      LastName = 'Test',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US',
      TimeZoneSidKey='America/Los_Angeles',
      ProfileId = p.id,
      UserName='test@unitingambition.com'
    );

    insert u;
  

      ApexPages.StandardController controller = new ApexPages.StandardController(preventivo);
      Ordine_Aziendale stdController = new Ordine_Aziendale(controller);

      stdController.account = l_acc;
      stdController.opportunita = opp; 
      stdController.utente = u; 
      
      


/********************************************** FINISH TEST   *************************************/      
 
   test.stopTest();

   }
  
 }

 
Paolo Magliocco 18Paolo Magliocco 18
Sorry I tried to insert both codes but I have same previous problem. 
I have 0% of code coverage.....
parth jollyparth jolly
Hi Paolo ,

Reposting the answer from ***Jen Bennett.****

Ran into this same issue and found this solution (http://salesforce.stackexchange.com/questions/24082/code-coverage-on-dev-console) that worked for me...

Goto setup -> Develop -> Apex Test Execution and click the Options button.

Make sure "Store Only Aggregated Code Coverage" is UNCHECKED

That fixed the issue for me.


Else please check this page as it contains of all the issues related to your problem ..

https://developer.salesforce.com/forums/?id=906F0000000947WIAQ

Thanks

Paarth Jolly