I'm attempting to create a test class for the following code

global class AttachmentCreate {
   webService static String AttachPDFToQuote(string Id) {
        string result = '';

         PageReference pageRef = new PageReference('/apex/SOMEQUOTETEMP?Id='+Id);
         Blob content = pageRef.getContent();
         QuoteDocument qdoc = new QuoteDocument(Document = content, QuoteId = Id);
         insert qdoc;

         return result;


Here is my test class code:

global class AttachmentCreateTest {
 static testMethod void AttachPDFToQuoteTTest() {

        string result = '';
        string Id = '0Q041000000Xafi';
         PageReference pageRef = new PageReference('/apex/SOMEQUOTETEMP?Id='+Id);
         Blob content = Blob.valueOf('UNIT.TEST');

         QuoteDocument doc = new QuoteDocument(Document = content, QuoteId = Id);
         insert doc;



The problem is, when I validate the code in the inbound change sets, I get the error that my class has zero code coverage. Why is that?

  • FYI you should leave your test class private. There's no reason some other class would have to reference this one.
    – Adrian Larson
    Jun 16, 2017 at 15:29

1 Answer 1


You don't call your method. Of course you don't get any coverage! You need to actually call it:

// you should create your own data for the test
Quote record = new Quote(/*required fields*/);
insert record;


// add assertions!
// this step is vital

A couple notes about how to write good unit tests:

  • Never hard-code your Id values.

    Avoid Hardcoding IDs

    When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail.

  • You should instead create test data and use those values

    Testing Best Practices


    • Set up test data:
      • Create the necessary data in test classes, so the tests do not have to rely on data in a particular organization.
      • Create all test data before calling the Test.startTest method.
      • Since tests don't commit, you won't need to delete any data.
  • The most important step in unit testing is to verify application behavior using assertions.

    Verify the results are correct

    Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests.

    A good way to tell if unit tests are properly verifying results is to look for liberal use of the System.assert() methods. If there aren’t any System.assert() method calls, then the tests aren’t verifying results properly. And, no, System.assert(true); doesn’t count.

  • It's not going to work quite right anyways; getContent is not supported in unit tests, even if you set an HttpCalloutMock.
    – sfdcfox
    Jun 16, 2017 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .