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
sapnasolankisapnasolanki 

Test Class Coverage

My apex class is as below -

 

public class QuoteController
{
    
    Account account {get;set;}
    Opportunity opportunity{get;set;}
    Quote__c quote;
    Design_Rev__c dr;
    public Boolean designRevFlag {get; set;}
    public Boolean psiDesignFlag {get; set;}
     String accId {get;set;}
     String accName {get;set;}
 

    
    
    public Account getAccount()
       {
        if(account == null) account = new Account();
          return account;
    }
       public Opportunity getOpportunity()
       {
        if(opportunity == null) opportunity = new Opportunity();
          return opportunity;
       }
       public Quote__c getQuote()
    {
        if(quote == null) quote = new Quote__c();
          return quote;
    }
     public Design_Rev__c getDesign_Rev()
    {
        if(dr == null) dr = new Design_Rev__c ();
          return dr;
    }

    public QuoteController()
    {
        opportunity = new Opportunity();
        account = new Account();
        quote = new quote__c();
        dr = new Design_Rev__c();
        
        //setting initial default values
        opportunity.Name = ApexPages.currentPage().getParameters().get('name');
        If(opportunity.Name == '')
            opportunity.Name = account.Name;
        //account.id = Apexpages.currentPage().getParameters().get('accId');
        opportunity.StageName = 'New Prospect';
        dr.Rev__c = 'A';
        dr.System__c = 'PW - Playworld Independent';
        dr.Units__c = 'Imperial';
        dr.Post__c = 'Steel-Footed';
        dr.Age_Group__c = 'ASTM 5-12';
        dr.Compliance_Criteria__c = 'ASTM-F1487-07';
        dr.Specifications__c = 'N/A';
        dr.Shade_MPH__c = '150';
        dr.Border_Timbers__c = 'N/A';
        dr.Certified_Drawings__c = 'N/A';
        dr.X2D_CAD_File__c = 'N/A';
        
       
        
        
    }
   
   
   public PageReference step1()
   {
        String aName = account.Name;
        System.debug('aName'+aName);
          return Page.opptyStep1;
   }

   public PageReference step2()
   {
      return Page.opptyStep2;
   }

   public PageReference step3()
   {
      return Page.opptyStep3;
   }


   
   
   //to copy billing info to shipping info
    public PageReference copyShippingInfo()
       {
           account.ShippingStreet = account.BillingStreet;
           account.ShippingCity = account.BillingCity;
           account.ShippingState = account.BillingState;
           account.ShippingCountry = account.BillingCountry;
           account.ShippingPostalCode = account.BillingPostalCode;
           account.ShippingCountry = account.BillingCountry;
        return Page.opptyStep1;
    }
    
    // This method cancels the wizard, and returns the user to the accounts tab
    
 
    public PageReference cancel()
    {
            PageReference opportunityPage = new Pagereference('/001/o');
            opportunityPage.setRedirect(true);
            return opportunityPage;
    }

   
    Public PageReference saveaccount()
       {
        return Page.opptyStep2;
    }
   
    Public PageReference saveOppty()
    {
    //    accId = ApexPages.currentPage().getParameters().get('accId');
//        System.debug('ID::'+accId);
        
        return Page.opptyStep3;
    }
   
    Public PageReference saveQuote()
    {
            if(quote.Designer__c == 'Playworld Designer')
            {
                
                return Page.opptyStep4;
            }
            /*if(quote.Designer__c == 'Playworld Designer')
            {
                designRevFlag = False;
                psiDesignFlag = True;
            }*/
            else
            {
                
                return Page.opptyStep5;
            }
    }
    
    Public PageReference saveDesignRev()
    {
        accId = ApexPages.currentPage().getParameters().get('accId');
        System.debug('ID in save design rev:'+accId);
        If(accId == NULL)
        {
            insert account;
            opportunity.accountId = account.id;
            opportunity.Name = account.Name;
        }
        else
        {
            Account a = [Select Id,Name from Account where Id = :accId];
            //System.debug('in else'+a[0]);
            
            /*Account a = new Account();
            try
            {
                a = [SELECT Id,Name from Account where Id = :accId];
            }
            catch(System.QueryException e)
            {
                System.debug('in catch'+e);
            
                
            }    */
            System.debug('in else'+a);
            opportunity.AccountId = accId;
            opportunity.Name = a.Name;
            //opportunity.AccountId = a.Id;
        }
        system.debug('outside of if and else');
        //opportunity.Name = name;
           insert opportunity;
       //    opportunity.AccountId = accId;
   //        update opportunity;
           
           system.debug(' oppty inserted'+opportunity.Id);
           quote.Project__c = opportunity.Id;
           insert quote;
           system.debug('quote inserted'+quote.Id);
           
           dr.Quote__c = quote.Id;
           dr.Project__c = opportunity.id;
           insert dr;
           system.debug('dr inserted'+dr.id);
           
        PageReference accountPage = new Pagereference('/001/o');
        accountPage.setRedirect(true);
        return accountPage;
        
    //    String ObjectPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
              
      //  PageReference p = new PageReference('/'+ObjectPrefix);
      //  p.setRedirect(true);
 
               
        //return p;  
        
    }
    
   public PageReference save()
   {
      insert account;

    
      opportunity.accountId = account.id;
      insert opportunity;


      PageReference opptyPage = new ApexPages.StandardController(opportunity).view();
      opptyPage.setRedirect(true);

      return opptyPage;
   }

}

 

And the test class is -

public class testQuoteController
{

   static testMethod void testing()
   {
           QuoteController qc = new QuoteController();
           qc.getAccount();
           qc.getDesign_Rev();
           qc.getOpportunity();
           qc.getQuote();
           //If(opportunity.Name == '')
           // opportunity.Name = 'test';
            
        qc.step1();
           qc.step2();
           qc.step3();
   
           Account a = new Account();
           a.BillingStreet = '123 main street';
           a.BillingCity = 'Test';
           a.BillingState = 'PA';
           a.BillingPostalCode = '12345';
           a.BillingCountry = 'USA';
   
       
           qc.copyShippingInfo();
           
           System.assertEquals(a.BillingStreet, '123 main street');
           qc.cancel();
           qc.saveaccount();
   //qc.saveDesignRev();
           qc.saveOppty();
         
        
        //qc.save();
        
       Quote__c q = new Quote__c();
       q.Designer__c = 'Agency';
       qc.saveQuote();
      // Account a1 = [select id,name from account where name like '%test%'];
      Account account = new Account();
       Opportunity o = new Opportunity();
       o.Name = 'test';
       o.CloseDate = Date.newInstance(2012,12,31);
       o.AccountId = a.id;
       o.StageName = 'New Prospect';
       o.Type = 'Direct Agency';
       account.BillingStreet = '123 main street';
           account.BillingCity = 'Test';
           account.BillingState = 'PA';
           account.BillingPostalCode = '12345';
           account.BillingCountry = 'USA';
           account.Name = 'test';
           
           
   }
   
   
}

How can i increase the code coverage. Right now it is 67%

dmchengdmcheng

The IDE should display which lines are not being tested, so you need to write more unit test code to create conditions where those lines are executed.

sapnasolankisapnasolanki

Thanks for the reply but i am not entirely sure how to write the test code for this. The lines which are not being tested are -

 

Public PageReference saveDesignRev()
    {
        accId = ApexPages.currentPage().getParameters().get('accId');
        System.debug('ID in save design rev:'+accId);
        If(accId == NULL)
        {
            insert account;
            opportunity.accountId = account.id;
            opportunity.Name = account.Name;
        }
        else
        {
            Account a = [Select Id,Name from Account where Id = :accId];
            //System.debug('in else'+a[0]);
            
            /*Account a = new Account();
            try
            {
                a = [SELECT Id,Name from Account where Id = :accId];
            }
            catch(System.QueryException e)
            {
                System.debug('in catch'+e);
            
                
            }    */
            System.debug('in else'+a);
            opportunity.AccountId = accId;
            opportunity.Name = a.Name;
            //opportunity.AccountId = a.Id;
        }
        system.debug('outside of if and else');
        //opportunity.Name = name;
           insert opportunity;
       //    opportunity.AccountId = accId;
   //        update opportunity;
           
           system.debug(' oppty inserted'+opportunity.Id);
           quote.Project__c = opportunity.Id;
           insert quote;
           system.debug('quote inserted'+quote.Id);
           
           dr.Quote__c = quote.Id;
           dr.Project__c = opportunity.id;
           insert dr;
           system.debug('dr inserted'+dr.id);
           
        PageReference accountPage = new Pagereference('/001/o');
        accountPage.setRedirect(true);
        return accountPage;
        
  
    }

dmchengdmcheng

Have you looked at the Visualforce Controller section of this page:

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

You need to instantiate your VF page, then your controller, then set values in your controller variables,  and finally execute the PageReference method.