+ Start a Discussion
shinerajbshinerajb 

Test code for a class

Hai,

 

I am not familiar with test class.Can anyone help me to write th test code for this class ?

 

 

public class Login{

public PageReference redirect() {

Integer total_records;
String email_login;
email_login=UserInfo.getUserName();
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
String endpoint = 'http://50.57.52.21/salesforce/Services/validate_user_login.php?id='+email_login;
request.setEndPoint(endpoint);
request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
JSONParser parser = JSON.createParser(response.getBody());

while (parser.nextToken() != null)
{
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'total_records'))
{
// Get the value.
parser.nextToken();
// Compute the grand total price for all invoices.
total_records=integer.valueof(parser.getText());
// total_records=
}

}
if( total_records>0)
{
return Page.mkt_ana;
}
else
{
return Page.loginfailure;
}
return null ;
}
}

HariDineshHariDinesh

Hi,

 

The below will not cover the complete class but definatly cover some part of the class, which might be helpfull for you.

 

@isTest
private class TestLogin
    {
        static testmethod PageReference testredirect()
        {
          Login ll = new Login();
          ll.redirect();
          return  Page.loginfailure;
        }
        }

 

 

Please find the below links which will help you understaning about writing test classes.

 

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

 

 

shinerajbshinerajb

Hai,

 

I tried this but ,it shows only  52 % code coverage

HariDineshHariDinesh

Hi,

 

Yes as i said above the class will not cover completely as there is Http protocol request exist as below

 

HttpResponse response = httpProtocol.send(request);

 

Currently me too looking to cover these types of test classes.

 

shinerajbshinerajb

 

 

Please update me,when you get a solution,its very urgent for me