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
Magical-zazaMagical-zaza 

I need help obtaining 100% code coverage on this test class

Hi Everyone, 

I need some help in obtaining 100% code coverage on my apex test class. I simply don't know what I need in order to make it run well so I can eventually deploy it to production. 

Help is much appreciated :) 

Apex Class:

Public class generateQuotePdfDocument{
    
    @InvocableMethod   
    public static void CreateQuote(List<Id> quoteIds)  
    { 
        createQuoteFutureMethod(quoteIds);
    }
    
    @future(callout=true)
    public static void createQuoteFutureMethod (List<Id> quoteIds) {
        //Initialize the quote url
        String quoteUrl = '/quote/quoteTemplateDataViewer.apexp?';
        
        //Get the Quote Template Id from Custom Settings
        String quoteTemplateId = Label.QuoteTemplateID;
        
        //List variable to get all the Quote Documents
        List<QuoteDocument> lstQuoteDoc = new List<QuoteDocument>();
        
        if(!quoteIds.isEmpty() && quoteIds.size() > 0) {
            
            for(Id quoteId :quoteIds) {
                //Construct the quote URL to generate PDF
                quoteUrl += 'id=' + quoteId;
                quoteUrl += '&headerHeight=197&footerHeight=10';
                quoteUrl += '&summlid=' + quoteTemplateId;
                
                //call the quote url
                PageReference pageRef = new PageReference(quoteUrl);
                
                //get the quotePdf
                Blob quoteBlob;
                
                if(Test.isRunningTest()) {
                    quoteBlob = Blob.valueOf('Generate Pdf');
                } else {
                    quoteBlob = pageRef.getContentAsPDF();
                }
                
                //initialze the QuoteDocument to hold the quote pdf for insertion
                QuoteDocument quoteDoc = new QuoteDocument();
                quoteDoc.Document = quoteBlob;
                quoteDoc.QuoteId = quoteId;
                lstQuoteDoc.add(quoteDoc);
            }
        }
        
        if(!lstQuoteDoc.isEmpty() && lstQuoteDoc.size() > 0) {
            Database.insert(lstQuoteDoc);
        }
        
    }
}

 

Apex Test Class

@istest
private class generateQuotePdfDocumentTest {
    
    @testSetup
    static void setup() {
    
        //Adjust the record type based on yoru setting
        RecordType rt = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND developerName = 'Opportunity'];
        
        Product2 product = new Product2();
        product.Name = 'LDPE Ecompound Black ';
        product.ProductCode = '6045200';
        product.IsActive = true;
        insert product;
        
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = Test.getStandardPricebookId();
        pbe.Product2Id = product.Id;
        pbe.IsActive = true;
        pbe.UnitPrice = 10;
        insert pbe;
       
        Account ac = new Account();
        ac.Name = 'Test';
        
        Opportunity op = new Opportunity();
        op.Name = 'Test';
        op.RecordTypeId = rt.Id;
        op.Type = 'Nieuw';
        op.Amount= 1200;
        op.CloseDate = Date.today().addDays(2);
        op.StageName = 'Actief';
        op.Probability = 10;
        op.LeadSource='Website';
        op.Name = 'test';
        op.Productgroep__c ='Ecompound' ;
        op.Description ='Test';
        op.Nextstep ='Test';
     
        insert op;
                
        Quote quote = new Quote();
        quote.OpportunityId = op.Id;
        quote.Name = 'TestQuote';
        quote.ExpirationDate = Date.today().addDays(5);
        quote.Status = 'Draft';
        quote.Pricebook2Id = Test.getStandardPricebookId();
        insert quote;
        
        QuoteLineItem qli = new QuoteLineItem();
        qli.QuoteId = quote.Id;
        qli.Quantity = 2;
        qli.PricebookEntryId = pbe.Id;
        qli.UnitPrice = 20;
        insert qli;
    }
    
    @isTest
    static void generateQuotePdfTest() {
        
        Quote quote = [SELECT Id, Status FROM Quote LIMIT 1];
        
        quote.Status = 'Draft';
        update quote;
    }
   
    
}

 

Best Answer chosen by Magical-zaza
CharuDuttCharuDutt
Hii Magical-zaza
Try Below Test Class
@istest
private class generateQuotePdfDocumentTest {
    
    @testSetup
    static void setup() {
    
        //Adjust the record type based on yoru setting
        RecordType rt = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND developerName = 'Opportunity'];
        
        Product2 product = new Product2();
        product.Name = 'LDPE Ecompound Black ';
        product.ProductCode = '6045200';
        product.IsActive = true;
        insert product;
        
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = Test.getStandardPricebookId();
        pbe.Product2Id = product.Id;
        pbe.IsActive = true;
        pbe.UnitPrice = 10;
        insert pbe;
       
        Account ac = new Account();
        ac.Name = 'Test';
        
        Opportunity op = new Opportunity();
        op.Name = 'Test';
        op.RecordTypeId = rt.Id;
        op.Type = 'Nieuw';
        op.Amount= 1200;
        op.CloseDate = Date.today().addDays(2);
        op.StageName = 'Actief';
        op.Probability = 10;
        op.LeadSource='Website';
        op.Name = 'test';
        op.Productgroep__c ='Ecompound' ;
        op.Description ='Test';
        op.Nextstep ='Test';
     
        insert op;
                
        Quote quote = new Quote();
        quote.OpportunityId = op.Id;
        quote.Name = 'TestQuote';
        quote.ExpirationDate = Date.today().addDays(5);
        quote.Status = 'Draft';
        quote.Pricebook2Id = Test.getStandardPricebookId();
        insert quote;
        
        QuoteLineItem qli = new QuoteLineItem();
        qli.QuoteId = quote.Id;
        qli.Quantity = 2;
        qli.PricebookEntryId = pbe.Id;
        qli.UnitPrice = 20;
        insert qli;
    }
    
      @isTest
    static void generateQuotePdfTest() {
        list<Id>lstQ =new list<Id>();
        Quote quote = [SELECT Id, Status FROM Quote LIMIT 1];
       
        quote.Status = 'Draft';
        update quote;
         lstQ.Add(quote.Id);
        generateQuotePdfDocument.CreateQuote(lstQ);
    }
   
    
}
Please Mark It As Best Answer If It Helps
Thank You! 

 

All Answers

CharuDuttCharuDutt
Hii Magical-zaza
Try Below Test Class
@istest
private class generateQuotePdfDocumentTest {
    
    @testSetup
    static void setup() {
    
        //Adjust the record type based on yoru setting
        RecordType rt = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND developerName = 'Opportunity'];
        
        Product2 product = new Product2();
        product.Name = 'LDPE Ecompound Black ';
        product.ProductCode = '6045200';
        product.IsActive = true;
        insert product;
        
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = Test.getStandardPricebookId();
        pbe.Product2Id = product.Id;
        pbe.IsActive = true;
        pbe.UnitPrice = 10;
        insert pbe;
       
        Account ac = new Account();
        ac.Name = 'Test';
        
        Opportunity op = new Opportunity();
        op.Name = 'Test';
        op.RecordTypeId = rt.Id;
        op.Type = 'Nieuw';
        op.Amount= 1200;
        op.CloseDate = Date.today().addDays(2);
        op.StageName = 'Actief';
        op.Probability = 10;
        op.LeadSource='Website';
        op.Name = 'test';
        op.Productgroep__c ='Ecompound' ;
        op.Description ='Test';
        op.Nextstep ='Test';
     
        insert op;
                
        Quote quote = new Quote();
        quote.OpportunityId = op.Id;
        quote.Name = 'TestQuote';
        quote.ExpirationDate = Date.today().addDays(5);
        quote.Status = 'Draft';
        quote.Pricebook2Id = Test.getStandardPricebookId();
        insert quote;
        
        QuoteLineItem qli = new QuoteLineItem();
        qli.QuoteId = quote.Id;
        qli.Quantity = 2;
        qli.PricebookEntryId = pbe.Id;
        qli.UnitPrice = 20;
        insert qli;
    }
    
      @isTest
    static void generateQuotePdfTest() {
        list<Id>lstQ =new list<Id>();
        Quote quote = [SELECT Id, Status FROM Quote LIMIT 1];
       
        quote.Status = 'Draft';
        update quote;
         lstQ.Add(quote.Id);
        generateQuotePdfDocument.CreateQuote(lstQ);
    }
   
    
}
Please Mark It As Best Answer If It Helps
Thank You! 

 
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

hi,

@isTest
public class generateQuotePdfDocumentTest {
    
    public static testMethod void CreateQuote(){

    // Insert Account

    Account a = new Account();
    a.Name = 'Test Account';
    insert a;
	
	Contact con = new Contact(lastname='test contact',email='test@gmail.com',phone='1284290842');
    insert con;

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
     insert standardPrice ;
	 
	 Opportunity op = new Opportunity();
        op.Name = 'Test Data';
        op.CloseDate = Date.today();
        op.StageName = 'Qualification';     
        insert op;
		
		
		 Quote qt = new Quote(Name='test quote',OpportunityId=op.id,Pricebook2Id = pricebookId, ContactId=con.id, ExpirationDate=system.today());
    insert qt;

    QuoteLineItem qliliner = new QuoteLineItem(Product2Id=p.id,QuoteId=qt.id,PriceBookEntryID=standardPrice,Quantity=4, UnitPrice =50);
    insert qliliner;
	List<Id> ListId=new List<Id>();
	ListId.add(qt.id);
	
	Test.StartTest();
	generateQuotePdfDocument.CreateQuote(ListId);
	Test.StopTest();
	
	}
	}

Thank You
Magical-zazaMagical-zaza
Massive thanks for the support!  Both test classes worked effortlessly! @ 95% coverage.