public string main(string url)

            Http h= new Http();    
            HttpRequest req= new HttpRequest();
            string hresult='';

               HttpResponse hres= h.send(req);
                hresult = hres.getBody();    
                system.debug('sysst' +hresult);          
                hresult = '<?xml version="1.0" encoding="ISO-8859-1"?><HelpTextDetails><HelpTab TabName="C&P InvoicePayment" label="C&P InvoicePayment"></HelpTab></HelpTextDetails>';
            return hresult;

My Test Class is

public static testmethod void helptest(){
        helptex hc = new helpTex();
         hc.isValid = true;

This is not covering this part

hresult = hres.getBody(); 
return result

Once this part is covered,I can cover the another method which is referred to this method.What should I do to cover this part also


The reason you are seeing those as not covered is because your unit test is throwing an error. From their documentation:

Apex Code has built in functionality to call external Web services, such as Amazon Web Services, Facebook, Google, or any publicly available web service. As a result, you will need to have the proper test method code coverage for the related Apex code that makes these callouts. But since the Force.com platform has no control over the external Web service and the impact of making the web service call, test methods can not invoke a 3rd party web service. This section provides a viable workaround to ensure proper code coverage.

Essentially, you can't call out to that service during a test. It looks like you tried to use this tutorial, but you didn't really follow it very well. You should not be setting hc.isValid = true;. That value should return false so you don't actually call out to the web service. In this scenario, you will never achieve 100% code coverage, the

HttpResponse hres = h.send(req);
hresult = hres.getBody();    
System.debug('syssss' +hres);
System.debug('sysst' +hresult);

section will not be tested no matter what you do.

As a final note, that unit test is not valid. You must assert something. Your unit test only validates that the code will not error out, which isn't what a unit test is all about. From the Salesforce documentation:

To facilitate the development of robust, error-free code, Apex supports the creation and execution of unit tests. Unit tests are class methods that verify whether a particular piece of code is working properly. Unit test methods take no arguments, commit no data to the database, send no emails, and are flagged with the testMethod keyword in the method definition.

Properly testing a piece of code will verify it does exactly what you originally intended for it to do. A good example of a proper unit test is provided here on Salesforce's documentation. Long story short, learn the System.assert, System.assertEquals, and System.assertNotEquals commands. Read up on their documentation here.



The above blog specifies how to use mockcallout interface and test the code .I think since salesforce has come out with these interfaces we should use these for tetsing callouts.

  • 1
    Correct, without using this interface - the test class will basically skip anything with a callout.
    – joshbirk
    Feb 20 '13 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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