+ 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 
                FROM Opportunity 
                WHERE Id IN :oppsId
                if (opp.Quotes.isEmpty()) continue;
                Quote theQuote = null;
                for (Quote quoteAux : opp.Quotes) {
                    if (quoteAux.IsSyncing) {
                        theQuote = quoteAux;
                if (theQuote == null) theQuote = opp.Quotes.get(0);
                PageReference pageRef = new PageReference(
                    quoteTemplateDataViewerUrl.replace('{!QuoteId}', theQuote.Id)
                    new QuoteDocument(
                        Document = pageRef.getContent(),
                        QuoteId = theQuote.Id
                 theQuote.Quote_Track__c= theQuote.Quote_Track__c+ 1;
                 update theQuote;
            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){


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.

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.
v varaprasadv varaprasad
Hi Umadevi,

Please check following code once.I have not tested below code may you will get syntactical errors.
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;

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;

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; 



Hope this helps you!

@For Support: varaprasad4sfdc@gmail.com