'How do I get code coverage in Salesforce?
I'm not a developer but found some info on GitHub which gave an Apex class that creates a Quote PDF upon creating the Quote. I tested in my sandbox and everything works perfectly, however it's telling me I have 0 code coverage so I can't deploy to Prod. Below are is the Apex and the Test for it. Any thoughts on what could be wrong? I followed all the steps in this article which provided the link to the code.
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);
}
}
}`
Test Class
`@istest
private class generateQuotePdfDocumentTest {
@testSetup
static void setup() {
Product2 product = new Product2();
product.Name = 'Test Product ';
product.ProductCode = '123';
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;
Opportunity op = new Opportunity();
op.Name = 'Test';
op.Type = 'Value Proposition';
op.Amount= 1200;
op.CloseDate = Date.today().addDays(2);
op.StageName = 'Created';
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;
}
}`
In my attempt to deploy to production I realized my code coverage was 0. So I'm trying to learn how to correct this issue.
Solution 1:[1]
You are not calling your code in your test method.
Sorry on my phone but something like
Test.startTest;
Generatequotepfddpcument.createquote(new list<id>{quote.id};
Test.StopTest;
Solution 2:[2]
How are you calling this invocable method in your class "CreateQuote"? I guess the the update you are doing in your test class is not firing that invocable method hence the 0 coverage. Try making the update so that the condition matches in your test class for firing the invocable method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Psymn |
| Solution 2 | Rohit Srivastava |
