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
DesaiDesai 

Code coverage for Standard controller test class

Hi ,

I have created a custom VF page to dipsplay list of quotes based on the quote status which is working but the Code coverage on test class is just 68%. 
VF Code
<apex:page StandardController="Quote" extensions="QuotePgeCtrl" sidebar="true" tabStyle="HPQuotes__tab">
    <html>
    <div class="blank">&nbsp;</div>
    <div class="content"><img src="/s.gif" class="pageTitleIcon" title="Quote"/>
    <h1>Quotes</h1><br>
    <font size="5">Home</font></br>
    <div class="blank">&nbsp;</div>
    </div></html>
     
    <apex:form >
        <apex:pageBlock title="Recent Quotes">
                <b>View:  </b>
                <apex:selectList size="1" value="{!quotevalue}">
                <apex:actionSupport event="onchange" action="{!getreviewdqt}" rerender="table1"/>
                <apex:selectOption itemLabel="None" itemValue="None"></apex:selectOption>
                <apex:selectOption itemLabel="Draft Quotes" itemValue="Draft Quotes"></apex:selectOption>
                <apex:selectOption itemLabel="Accepted Quotes" itemValue="Accepted Quotes"></apex:selectOption>
                <apex:selectOption itemLabel="Review Pending Quotes" itemValue="Review Pending Quotes"></apex:selectOption>
                </apex:selectList>
                
               <div style="width:100%; margin-top:5px;height:450px; overflow:scroll;">
                <apex:pageBlockTable id="table1" value="{!quoteList}" var="quotes" >
                <apex:column >
                <apex:facet name="header">Name</apex:facet>
                 <a href="#" onClick="window.parent.location.href='/{!quotes.Id}'">{!quotes.Name}</a>   
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Quote Number</apex:facet>
                 <a href="#" onClick="window.parent.location.href='/{!quotes.Id}'">{!quotes.QuoteNumber}</a>   
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Account Name</apex:facet>
                <a href="#" onClick="window.parent.location.href='/{!quotes.Opportunity.Account.Id}'">{!quotes.Opportunity.Account.name}</a>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Opportunity Name</apex:facet>
                <a href="#" onClick="window.parent.location.href='/{!quotes.Opportunity.Id}'">{!quotes.Opportunity.name}</a>
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Status</apex:facet>
                {!quotes.Status}
                </apex:column>
                
                <apex:column >
                <apex:facet name="header">Expiration Date</apex:facet>
                <apex:outputText value="{0,date,MM/dd/yyyy}">
                        <apex:param value="{!quotes.ExpirationDate}"/>
                    </apex:outputText>  
                </apex:column>
  
                <apex:column ><apex:facet name="header">Grand Total</apex:facet>
                <apex:outputText value="{0, number, currency}">
                        <apex:param value="{!quotes.GrandTotal}"/>
                    </apex:outputText> 
                </apex:column>
           
                </apex:pageBlockTable>
                </div>
         </apex:pageBlock>
    </apex:form>
</apex:page>

SC class

public class QuotePgeCtrl
{
   
    public Id quoteId {get;set;}
    public QuotePgeCtrl(ApexPages.StandardController controller)
    {
        sObject quote = controller.getRecord();
        quoteId = (Id) quote.get('Id');
    }

    public string quotevalue{get; set;}
    public list<Quote> quoteList{get; set;}
    public list<Quote> getreviewdqt()
    {
        if(quotevalue == 'None')
       {
            quoteList= new list<Quote>([SELECT Opportunity.Account.Name, QuoteNumber, GrandTotal, Opportunity.name,OpportunityId, Name, IsSyncing, Id, Status,ExpirationDate From Quote WHERE Status ='None']);
      }
       if(quotevalue == 'Draft Quotes')
       {
           quoteList= new list<Quote>([SELECT Opportunity.Account.Name, QuoteNumber, GrandTotal, Opportunity.name,OpportunityId, Name, IsSyncing, Id, Status,ExpirationDate From Quote WHERE Status ='Draft']);
      }
       if(quotevalue == 'Accepted Quotes')
       {
            quoteList= new list<Quote>([SELECT Opportunity.Account.Name, QuoteNumber, GrandTotal, Opportunity.name,OpportunityId, Name, IsSyncing, Id, Status,ExpirationDate From Quote WHERE Status ='Accepted']);
       }
        if(quotevalue == 'Review Pending Quotes')
       {
            quoteList= new list<Quote>([SELECT Opportunity.Account.Name, QuoteNumber, GrandTotal, Opportunity.name,OpportunityId, Name, IsSyncing, Id, Status,ExpirationDate From Quote WHERE Status ='Review Pending']);
       }
      return null;
     }
 }

Test Class
@isTest
public class QuotePgeCtrlTestClass
{
    static testMethod void testMethod1()
    {
        string quotevalue = 'Draft Quotes';
        Quote testQuote = new Quote();
        testQuote.Name='TestQuote' ;
        testQuote.ExpirationDate = system.today();
        testQuote.Status = 'Draft';
        testQuote.OpportunityId = '0066300000341BJ' ;
        System.debug('******************Inserting the record...');
        insert testQuote;
       Test.StartTest();
       
       ApexPages.StandardController sc = new ApexPages.StandardController(testQuote);
        QuotePgeCtrl testQt = new QuotePgeCtrl(sc);
      list<Quote> obj = testQt.getreviewdqt();
       Test.StopTest();
}

What is missign in the code ?

Thanks & regards,
Pallavi
Best Answer chosen by Desai
jakin prajapatijakin prajapati
HI Pallavi,

Please try Below code you get 100% code coverage....
@istest
public class QuotePageCtrl_UT {
    public static testmethod void TestMethod1(){
                      
        Opportunity objOpp = new Opportunity();
        objOpp.Name = 'testOpp';
        objOpp.StageName = 'Closed Won';
        objOpp.CloseDate = date.parse('10/10/2016');
        insert objOpp;
   
        Quote objQoute= new Quote();
        objQoute.Name='TestQuote' ;
        objQoute.ExpirationDate = date.parse('12/10/2016');
        objQoute.Status = 'None';
        objQoute.OpportunityId = objOpp.Id ;
        insert objQoute;
     
        QuotePgeCtrl objQuoteClass = new QuotePgeCtrl(new ApexPages.StandardController(objQoute));
        list<Quote> listQuote = objQuoteClass.quoteList ;
        
        objQuoteClass.quotevalue = 'None';
        system.assertEquals('None',objQuoteClass.quotevalue);
        objQuoteClass.getreviewdqt();
        objQuoteClass.quotevalue = 'Draft Quotes';
        system.assertEquals('Draft Quotes',objQuoteClass.quotevalue);
        objQuoteClass.getreviewdqt();
        objQuoteClass.quotevalue = 'Accepted Quotes';
        system.assertEquals('Accepted Quotes',objQuoteClass.quotevalue);
        objQuoteClass.getreviewdqt();
        objQuoteClass.quotevalue = 'Review Pending Quotes';
        system.assertEquals('Review Pending Quotes',objQuoteClass.quotevalue);
        objQuoteClass.getreviewdqt();
	}
}


Thanks & Cheers.
Jakin
 

All Answers

hai.huanghai.huang
Test data seems not enough.
@isTest
public class QuotePgeCtrlTestClass
{
    static testMethod void testMethod1()
    {
        string quotevalue = 'Draft Quotes';
        Quote testQuote = new Quote();
        testQuote.Name='TestQuote' ;
        testQuote.ExpirationDate = system.today();
        testQuote.Status = 'Draft';
        ...
    }

    static testMethod void testMethod2()
    {
        string quotevalue = 'Accepted Quotes';
        Quote testQuote = new Quote();
        testQuote.Name='TestQuote' ;
        testQuote.ExpirationDate = system.today();
        testQuote.Status = ''Accepted';
        ...// same as testMethod1
        
    }
    static testMethod void testMethod3()
    {
        string quotevalue = 'Review Pending Quotes';
        Quote testQuote = new Quote();
        testQuote.Name='TestQuote' ;
        testQuote.ExpirationDate = system.today();
        testQuote.Status = ''Review Pending';
        ...// same as testMethod1
    }
    ...
}
If run test in develop console, please check tests tab and find test class in right bar, then double click it.
You will find which part isn't be covered(not in blue color).
Hope it will a help.
Best,
Huanghttps://randsfdc.blogspot.com (https://randsfdc.blogspot.com" target="_blank)
 
Banwari kevat1Banwari kevat1
In Test Class, statement   
testQuote.OpportunityId = '0066300000341BJ' ;
is not acceptable because we can't assign hardcoded id for a record.For OpportunityId you have to first create a opportunity record then insert it and then assign this opportunity id to testQuote.OpportunityId.

Regards
Banwari kevat
jakin prajapatijakin prajapati
HI Pallavi,

Please try Below code you get 100% code coverage....
@istest
public class QuotePageCtrl_UT {
    public static testmethod void TestMethod1(){
                      
        Opportunity objOpp = new Opportunity();
        objOpp.Name = 'testOpp';
        objOpp.StageName = 'Closed Won';
        objOpp.CloseDate = date.parse('10/10/2016');
        insert objOpp;
   
        Quote objQoute= new Quote();
        objQoute.Name='TestQuote' ;
        objQoute.ExpirationDate = date.parse('12/10/2016');
        objQoute.Status = 'None';
        objQoute.OpportunityId = objOpp.Id ;
        insert objQoute;
     
        QuotePgeCtrl objQuoteClass = new QuotePgeCtrl(new ApexPages.StandardController(objQoute));
        list<Quote> listQuote = objQuoteClass.quoteList ;
        
        objQuoteClass.quotevalue = 'None';
        system.assertEquals('None',objQuoteClass.quotevalue);
        objQuoteClass.getreviewdqt();
        objQuoteClass.quotevalue = 'Draft Quotes';
        system.assertEquals('Draft Quotes',objQuoteClass.quotevalue);
        objQuoteClass.getreviewdqt();
        objQuoteClass.quotevalue = 'Accepted Quotes';
        system.assertEquals('Accepted Quotes',objQuoteClass.quotevalue);
        objQuoteClass.getreviewdqt();
        objQuoteClass.quotevalue = 'Review Pending Quotes';
        system.assertEquals('Review Pending Quotes',objQuoteClass.quotevalue);
        objQuoteClass.getreviewdqt();
	}
}


Thanks & Cheers.
Jakin
 
This was selected as the best answer
DesaiDesai
Hi Jakin,

Thanks for inputs. Above code is perfect.

Regards,
Pallavi
jakin prajapatijakin prajapati
HI Pallavi,

You'r Welcome any time ☺...

Thanks,
Jakin