function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Katharine Anderson 13Katharine Anderson 13 

Test code for webservices method

Okay, I think I'm a little over my head and I could use some guidance. This is one of my first major coding projects and I have hit a wall.
 
My goal is for my users to be able to look at a listview, select several records, and then click a button that will create pdfs from those records, attach them to the records, and then take the attachment and upload it to the Box folder associated with that record's parent.
 
I have uploaded all of my code to this gist: https://gist.github.com/AndersonKatharineTNC/fe722b94215efe76d7be7b0d807d9e5b
 
I have two major issues that I would like help solving:
 
1) Test_PdfGeneratorController errors out because the savePdf() method in PdfGeneratorController2 is a webservices method and it needs do do a mock webservices callout. I generated the wsdl2apex class for PdfGeneratorController2, and I created a test class called WebServiceMockImpl which does the mock response to the callout, but I can't figure out how to get the mock callout to happen in Test_PdfGeneratorController in order to cover the savePdf() method.
 
2) The Attach Monitoring PDF button currently only works as a detail page button. I'd like to update the code in PdfGeneratorController2 to be able to iterate through several selected records in a listview and create attachments for each of them.
 
So, any suggestions? And thanks in advance for taking the time to look at this (sorry if my code is ugly or not well commented. I'll try to update the gist with some better comments soon!)
Best Answer chosen by Katharine Anderson 13
Glyn Anderson 3Glyn Anderson 3
I may have made a type in my one line of code.  Use WebServiceMock.class, not WebServiceMockImpl.class.  That should expose the mock callout.

As for the error, if the method is static, be sure to invoke it as such:

PdfGeneratorController2.savePdf( someId, someString );

It won't work as just:

savePdf( ... );

or 

myController.savePdf( ... );

All Answers

Glyn Anderson 3Glyn Anderson 3
Katharine, I haven't looked at your code yet.  Re: #1, do you call Test.SetMock in your test code?

<pre>
Test.SetMock( WebServiceMockImpl.class, new WebServiceMockImpl() );
</pre>
Katharine Anderson 13Katharine Anderson 13
Hi Glyn! Thanks for the quick response. No, I didn't have that. I just added it to my test code, but it didn't resolve the issue with the savePdf() method. I'm guessing that I have to reference it somehow when calling that method in the test. Researching to see if I can find the syntax for that now...
Katharine Anderson 13Katharine Anderson 13
I'm now getting this error: Static method cannot be referenced from a non static context: void PdfGeneratorController2.savePdf(Id, String)
Glyn Anderson 3Glyn Anderson 3
I may have made a type in my one line of code.  Use WebServiceMock.class, not WebServiceMockImpl.class.  That should expose the mock callout.

As for the error, if the method is static, be sure to invoke it as such:

PdfGeneratorController2.savePdf( someId, someString );

It won't work as just:

savePdf( ... );

or 

myController.savePdf( ... );
This was selected as the best answer
Katharine Anderson 13Katharine Anderson 13
Awesome, I think I got that piece. It's saving and the tests are running fine now, and I have 100% code coverage on PdfGeneratorController2!