Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
 public string main(string url)
    {

            Http h= new Http();    
            HttpRequest req= new HttpRequest();
            req.setEndpoint(url);
            req.setMethod('GET');
            req.setTimeout(60000);
            string hresult='';

            if(isValid==true)
            {
               HttpResponse hres= h.send(req);
                hresult = hres.getBody();    
                system.debug('syssss'+hres);
                system.debug('sysst' +hresult);          
           }else{
                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;
         hc.main('https://s3.amazonaws.com/clickandpledge/Salesforce/Help/Help.XML');
  }

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

share|improve this question
    
This is not much different from your other question: salesforce.stackexchange.com/questions/8523/… Had you reviewed the answer you have received there ? –  Samuel De Rycke Feb 20 '13 at 14:19
    
oh thanks @Sdry .I dont remember these days.ya i was also wondering like i remember i answered this some time ago. –  Mohith Shrivastava Feb 20 '13 at 15:52

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer

http://blogs.developerforce.com/developer-relations/2012/10/testing-http-callouts-with-static-data-in-winter-13.html#comment-688228232

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.

share|improve this answer
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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