+ Start a Discussion
RCJesseRCJesse 

Testing a static Webservice and testing a apex method called by a visual force page

I have two classes that I need to deploy to my production server but I can't because I don't have tests for them and am not sure how to test them.

 

The first, ConfirmSale, contains a WebService static method. How can I call that in a test class?

 

The second, ConvertLead, is a class I wrote to overwrite the lead convert button. My button calls a visualforce page which runs a method in the class. The issue here is that the method uses ApexPages.currentPage() to get parameters of the page that the user is looking at. So how do I replicate that for a test?

 

I need to deploy these by tomorrow so any help is appreciated. I have no problem posting the code if that helps.

jhenningjhenning

You can call a webservice static method within force.com the same way you would for any other static method. Just specifiy the classname and method name. ie. Boolean isConfirmed = ConfirmSale.myWebServiceMethod(stuff);

My example assumes that your method returns a boolean, just use whatever datatype it really returns.

 

For your second issue, I need some additional information. Can you give more specifics on ConvertLead? Post a code snippet?

RCJesseRCJesse

Hi John,

 

First off thanks for helping me, we are deploying our salesforce integration tomorrow and i need these two classes.

 

For my webservice static method, this is the error I get when I try to just call the method from my test class:

Save error: Static methods cannot be invoked through an object instance: confirm(Id)

 

My test class looks like this, my webservice static method returns void:

private class ConfirmSaleTest {

   static testMethod void myUnitTest() {

      Opportunity o = new Opportunity();

      o.RC_Account_Number__c = '6504724063';      ConfirmSale cs =

new ConfirmSale();

      cs.confirm(o.Id);

}

}

 

 

For the second issue. ConvertLead overrides the Opportunity Convert button. The button calls a visualforce page which using this code: <apex:page standardController="Lead" extensions="ConvertLead" action="{!autoRun}">

The ConvertLead class itself starts like this:

public class ConvertLead {

   // Constructor - this only really matters if the autoRun function doesn't work right

    private final Lead l;

   public ConvertLead(ApexPages.StandardController stdController) {

      this.l = (Lead)stdController.getRecord();

   }

 

   // Code we will invoke on page load.

    public PageReference autoRun() {       String theId = ApexPages.currentPage().getParameters().get('id');

 

        if (theId == null) {

          // Display the Visualforce page's content if no Id is passed over

              return null;

       }

....

 

Essentially the autoRun() method of ConverLead doesn't take any paramenters but obtains the Id of the opportunity on the screen and goes from there. So the question is how do I replicate having an open screen in a test.

 

 

jhenningjhenning

RCJesse, Glad to help. For your first question, the problem is that you are instantiating a class to call a static method. When a method is marked as Static, you do not instantiate the class first. Delete "ConfirmSale cs = new ConfirmSale();"

Simply call the method "ConfirmSale.Confirm(o.Id); Also, if Confirm returns a value, use the System.AssertEquals method to test the output.

 

For your second problem, in a test method, you should be able to test it as follows:

//Create a controller object
ApexPages.standardController controller = new ApexPages.standardController(new Lead());
//Get a random lead for the query string parm
ApexPages.currentPage().getParameters().put('Id',[select id from Lead limit 1].id);
//Create the extension object
ConvertLead myExtension = new ConvertLead(controller);
string myURL = myExtension.autoRun().getUrl();
 
Then test the value of myURL to see if it matches the URL that you are expecting. let me know if that helps.

 

 

RCJesseRCJesse

John, you are a king! I got both classes deployed just barley as per the code coverage results.

 

My code coverege on my second class though is very low. Seems like after I make my initial Http send none of the lines are called. The deployment results basically show every line number after "HTTPResponse res = h.send(req);" as not being called. Any thoughts?

jhenningjhenning
Excellent! I'm glad it worked out. For additional code coverage on your controller extension class, make sure you are testing all of your action methods that you have defined in the class.
RCJesseRCJesse

Sorry I confused myself I meant that the first class had a low code covereage, not the controller extension class but the webservice class.

 

It only has one method which is called but at a certain point the code just stops running. In the code snippet below every line after the one underlined is not called. Is it maybe because the underlines line calls out to an exernal service and the test doesnt wait for it?

 

global class ConfirmSale {

 

WebService static void confirm(string oppID) {

   String code;

   String success; 

   Opportunity o = [select Id,RC_Account_Number__c, description from Opportunity where id =:oppID];

    //use api to get userID by way of account number

    // request authorization

    HttpRequest req = new HttpRequest();

    req.setEndpoint('http://******');

    req.setMethod('POST');

    req.setHeader('Host', '******');    req.setHeader(

'SOAPAction', '');

    req.setHeader('User-Agent', 'Jakarta Commons-HttpClient/3.0.1');    req.setHeader(

'Content-Type', 'text/xml;charset=UTF-8');    req.setBody('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:aut="http://service.ringcentral.com/Authenticator/"> <soapenv:Header/> <soapenv:Body> <aut:partnerLogin> <login>***</login> <password>***</password> </aut:partnerLogin> </soapenv:Body> </soapenv:Envelope>');

 

    http h = new Http();

    HTTPResponse res = h.send(req);

 

    String cookie = res.getHeader('Set-Cookie');

 

    // call method

    HttpRequest reqID = new HttpRequest();

    ...

jhenningjhenning

I ran a test and found out a little tidbit of information:

 

System.TypeException: Testmethods do not support webservice callouts.

 

So the testmethod will not run the web service callout. That's news to me but that's why your coverage was not 100%.

RCJesseRCJesse

Thats the conclusion I came to also, although I left it untested :)

 

Thanks again John.