This question already has an answer here:

Hello I am new to salesforce I have worked on apex code and Visualforce page. Which displays the some fields in PDF of the current record.My test code is not showing any errors, when I check the Apex class code coverage is 0 %. Any help would be great. Thanks in Advance

Apex code:

public class GeneratePDf 
    public SVMXC__Quote__c acc{get;set;}

    public GeneratePDf(ApexPages.StandardController controller) 

        acc=[ SELECT Id,  Name, SVMXC__Status__c, Deposit__c, Created_BY__c, 
                SVMXC__Quote__c.CreatedDate,   Balance_Owed__c,    Unit1__r.Name,     
                Property_Owner__c,  Property_Address__c,Description__c ,   Vendors__r.Name,
                Vendor_Fax__c, Purchase_Order_Amt__c, General_Liability_Expires__c,  
                Workers_Compensation_Expires__c, Vendor_City__c, Vendor_Street__c,  Vendor_State__c, 
            FROM SVMXC__Quote__c 
            WHERE Id =:ApexPages.currentPage().getParameters().get('id')];


Test Code:

public class GeneratePDf_Test{

    static void setupTestData(){


        SVMXC__Quote__c ssr = new SVMXC__Quote__c();
        ssr.Unit__r.Name = '100';
        ssr.Id = 'a0O2F000000mwXK';
        ssr.Vendor_Phone__c ='123-458682';
        ssr.Vendor_Email__c = 'test@gmail.com ';
        ssr.Deposit__c = 33;
        ssr.Vendor_State__c = 'Ohio';
        ssr.Vendor_Zip__c = '07306';
        ssr.Property_Owner__c = 'a0O2F000012mwXK';
        ssr.Description__c = 'test';
        ssr.Vendor_Fax__c = '12345y';
        ssr.General_liability_Expires__c = date.today();
        ssr.Workers_Compensation_Expires__c = date.today();
        ssr.Vendor_City__c = 'Perth';
        ssr.Vendor_Street__c ='wales ave';

        insert ssr;   


marked as duplicate by glls, David Reed, Raul, Samuel Dare, Oleksandr Berehovskyi Apr 30 '18 at 7:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    Welcome to SFSE, you might want to check Get Started with Apex Unit Tests Unit on Trailheads to get a better understanding of how to unit tests work. Also, makse sure you check out How to Ask and take the tour. Please format your code when posting on the site, thanks – glls Apr 28 '18 at 20:04
  • 1
    I've formatted your code for you this time. Basically, to format a bunch of code as a single block, you need to make sure that every line of code has at least 4 blank spaces at the start of every line. The easy way to do this is to highlight your entire code block and press ctrl + k. Also, indentation is important. It helps people to read and follow your code (when asking other people for help, it's important that they can easily read and understand your code). Some blank lines between code helps readability, but a blank line between every line is just a waste of space (in my opinion). – Derek F Apr 28 '18 at 21:49

The reason why your class is showing 0% coverage is because you are not really testing your controller class from the test class that you have written. Your test class is just inserting the records and doing nothing. For your controller class to have the coverage, you will need to "functionally" test your class by calling the methods in that class. You should definitely start looking more on how to write good unit tests in the resources available, and a good starting point would be this module on trailhead.

Without getting into details of a how to write a good unit test class, which you should definitely read through because in your current test class you have quite a few of issues, e.g., you don't have any assertions, your setup method is not really a setup method, etc., and considering only what you have written now, below are details of how you can cover your controller class.

You need to basically create an instance of GeneratePDf class from your test class. Keeping all other code as you have currently, you will need to additionally add the following in your test method to cover your controller class:

//existing code
insert ssr;

// this line is required to set the id parameter as you are fetching that in your controller's constructor
ApexPages.currentPage().getParameters().put('id', ssr.Id);
GeneratePDF pdf = new GeneratePDF(new ApexPages.StandardController(ssr));

// add assertions


Not the answer you're looking for? Browse other questions tagged or ask your own question.