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
Umadevi SUmadevi S 

how write test class for apex class with 75% Percentage Code Coverage


Hi Team,

I am create the apex class for tracking the Quote.....
My Apex Class is
global with sharing class InvoicePdfWsSample {
    webService static String generateInvoicePdf(List<Id> oppsIdList) {
        try {
            final Set<Id> oppsId = new Set<Id>(oppsIdList);
            final String quoteTemplateDataViewerUrl = '/quote/quoteTemplateDataViewer.apexp?id={!QuoteId}&headerHeight=100&footerHeight=100&summlid=0EH0K0000034xt6';
            if (String.isBlank(quoteTemplateDataViewerUrl)) {
                String errorMsg = 'Invoice Template Id or Quote Template Data Viewer URL are blank, please review their values in Application Properties custom setting.'; 
                return errorMsg;
            }
            
            final List<QuoteDocument> attList = new List<QuoteDocument>();
            
            for (Opportunity opp : 
                [
                SELECT Id, 
                    (
                        SELECT Id, QuoteNumber, IsSyncing,Quote_Track__c,CreatedDate 
                        FROM Quotes 
                        ORDER BY CreatedDate 
                        DESC
                    ) 
                FROM Opportunity 
                WHERE Id IN :oppsId
                ]) 
            {
                if (opp.Quotes.isEmpty()) continue;
                Quote theQuote = null;
                for (Quote quoteAux : opp.Quotes) {
                    if (quoteAux.IsSyncing) {
                        theQuote = quoteAux;
                        break;
                    }
                }
                if (theQuote == null) theQuote = opp.Quotes.get(0);
                PageReference pageRef = new PageReference(
                    quoteTemplateDataViewerUrl.replace('{!QuoteId}', theQuote.Id)
                );
 
                attList.add(
                    new QuoteDocument(
                        Document = pageRef.getContent(),
                        QuoteId = theQuote.Id
                    )
                );
                 theQuote.Quote_Track__c= theQuote.Quote_Track__c+ 1;
                 update theQuote;
            }
 
            system.debug(attList);
            if (!attList.isEmpty()){
                 insert attList;
            }             
            return '';
        } catch (Exception e) { System.debug(LoggingLevel.ERROR, e.getMessage());  final String errorMsg = 'An error has occured while generating the invoice. Details:\n\n' + e.getMessage() + '\n\n' + e.getStackTraceString(); return errorMsg; }
    }
}


My Test Class
@isTest (SeeAllData=true)                    
public class InvoicePdfWsSampleTest
{
    static testMethod void TestMe()
    {
        List<Id> oidl = new List<Id>();
        List<Opportunity> ol = new List<Opportunity>([SELECT Id FROM Opportunity ORDER BY CreatedDate DESC LIMIT 10]);
        for (Opportunity o : ol){
            oidl.add(o.Id);
        }
        InvoicePdfWsSample.generateInvoicePdf(oidl);
        
    }

    }

I getting 69% Code coverage of the test class. How can i increase the code coverage?
 
Prakash NawalePrakash Nawale
Hi  Umadevi S,

Please remove SeeAllData=true from class level.

Create your own test data in your test class.
Please follow below link for more details and mark this as best answer if it helps to you.
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Regards,
Prakash
nawaleprakash@gmail.com
Umadevi SUmadevi S
Hi Prakash,

when i remove it. It show 35%
Prakash NawalePrakash Nawale
Yes, You have to create test records in your test methods, can you please go through below link, it will help you to build this.
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
 
v varaprasadv varaprasad
Hi Umadevi,

Please check following code once.I have not tested below code may you will get syntactical errors.
 
@isTest                    
public class InvoicePdfWsSampleTest
{
    static testMethod void TestMe()
    {
	
	set<id> setIds = new set<id>();

Opportunity opp = new Opportunity(
  Amount = 100000.00,                   // Amount
  IsPrivate = false,                      // Private
  ExpectedRevenue = 10000.00,             // Expected Revenue
  Name = 'Pyramid Emergency Generators',  // Opportunity Name  
  StageName = 'Prospecting',              // Stage
  LeadSource = 'Phone Inquiry'           // Lead Source  
);
insert opp;
setIds.add(opp.id);

Opportunity opp1 = new Opportunity(
  Amount = 10000.00,                   // Amount
  IsPrivate = false,                      // Private
  ExpectedRevenue = 1000.00,             // Expected Revenue
  Name = 'Pyramid Emergency Generators1',  // Opportunity Name  
  StageName = 'Prospecting',              // Stage
  LeadSource = 'Phone Inquiry'           // Lead Source  
);
insert opp1;
setIds.add(opp1.id);


Qutote qt = new Qutote()
qt.Opportunityid = opp.id;
qt.IsSyncing = True;
qt.Quote_Track__c = 1;
insert qt; 

Qutote qt1 = new Qutote()
qt1.Opportunityid = opp1.id;
qt1.IsSyncing = True;
qt1.Quote_Track__c = 1;
insert qt1; 

        InvoicePdfWsSample.generateInvoicePdf(setIds);
        
    }

    }

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com