• Magical-zaza
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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;
    }
   
    
}

 

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;
    }
   
    
}