1

I'm new to development, but Ive spend more than 12 hours trying to figure this out and am tearing my hair out :( I've read callout and webservice testing and dont understand how it works...

So I have this class which I modified from: http://www.valnavjo.com/blog/how-to-create-invoices-using-quote-templates/

and I am not getting the coverage (only 23%) , so please offer any advice on how to proceed forward. Basically, the Webservice method that is called from a custom button on opportunity page to generate an invoice PDF file using quote templates feature.

Below is my test class and areas lacking coverage:

@isTest
public class TestInvoicepdfclass {
  static testMethod void testpdfbutton(){

 Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true );
    insert pb;
Product2 prod = new Product2(Name = 'SLA: Bronze', IsActive = true);
    insert prod;

PricebookEntry pbe=new PricebookEntry(unitprice=0.01,Product2Id=prod.Id, Pricebook2Id=Test.getStandardPricebookId(), IsActive= true); 
     insert pbe; 
      Test.startTest();
           // insert opp
        Account acc = new Account (name='Acme');
        insert acc;
        Opportunity opp= new Opportunity (name='Testopp', Accountid=acc.id, CloseDate= date.today(), StageName='Closed Won', Pricebook2id=Test.getStandardPricebookId());
          insert opp; 

OpportunityLineItem oppLine = new OpportunityLineItem( pricebookentryid=pbe.Id,TotalPrice=2000, Quantity = 2,Opportunityid = opp.Id);
insert oppLine;       
        // insert quote
        Quote q= new Quote (Name='Testq', Opportunityid=opp.id, QuotetoInvoice__c= True, REP__C= 'AC', Pricebook2id=Test.getStandardPricebookId());
           insert q;
        //add to list
         List<id> oppids= new List<id> ();
        oppids.add(opp.Id); 

          InvoicepdfWsSample.generateInvoicepdf(oppids);

         } 
}

1of2

1 Answer 1

6

The issue is not unique to "testing a webservice method" it is an error in your code. You test this webservice method just like any other method and can essentially ignore the webservice keyword for testing purposes.

Not sure why you did not post the error message as it would have been helpful and is the real culprit.

This line

final string invoiceTemplateId = Application_Properties__c.getAll().get('Invoice_Template_ID__c').value__c

Is most likely throwing a Dereference null object error because there are not records for the Application_Properties__c custom setting.

You will need to create the custom setting records in your test. Something like:

Application_Properties__c customSetting = New Application_Properties__c( 
                             Name = 'Invoice_Template_ID__c', 
                             Value__c = '[YOUR VALUE HERE]'
);
Insert customSetting;

Also, I do not believe that your use of the final keyword is appropriate in a class method. Maybe, it is for your case but I do not see a value in it.

4
  • Here is the part I dont understand, I added that information of Application properties custom setting. And then I added this: system.assert(!opp.Quotes.isempty()) and it returns with System.AssertException: Assertion Failed How is this possible when Ive inserted the quote??? and Im not getting any error message, just 23% coverage... even with Application properties custom setting. Its not making any difference....
    – Sammy
    Sep 30, 2016 at 16:13
  • Are you expecting data created outside of test method to be there during testing. If so that is not the case. You need to create all test data during testing. As for the rest of it, the answer will depend on seeing your code, logs, and steps to reproduce.
    – Eric
    Sep 30, 2016 at 19:16
  • But here is part I don't get..... when I put all the data or not in the test class it is not making a difference. For instance, if I remove everything from the test class except for this line: invoicepdfwsaample.generateinvoice (oppid) .... this alone gets me 23% coverage and all the rest of data I'm creating in test class is not affecting coverage....that's where I am lost. So I'm wondering whether I need a Test.mock.... but I have no idea how to write that for this controller.
    – Sammy
    Oct 1, 2016 at 13:26
  • No mock implementation needed. It is throwing an error at that line and must be caught somewhere or your test would be failing.
    – Eric
    Oct 1, 2016 at 14:40

You must log in to answer this question.

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