+ Start a Discussion
Avinash Ravisankar 13Avinash Ravisankar 13 

Require help developing a test class for this Extension

Hi,

Below are my main class and test class. I've only been able to achieve 49% coverage so far. Can someone help out please?

Apex Class:
public class FormController {
public FormController() {
    }
//public string email ;
public Apexpages.StandardController controller;
public Apexpages.StandardController control;
public contact con {get;set;}

public FormController (ApexPages.StandardController stdController) {
this.con = (contact)stdController.getRecord();
this.controller = stdController;
}


    public String FirstName{ get; set; }
    public String LastName{ get; set; }
    public String email{ get; set; }
    public String Phone{ get; set; }
    public String inputTextErrorMessage {get; set;}
    public String inputTextErrorMessage2 {get; set;}
    public String inputTextErrorMessage3 {get; set;}
    public String inputTextErrorMessage4 {get; set;}
    public String inputTextErrorMessage5 {get; set;}
    public String inputTextErrorMessage6 {get; set;}
    public String inputTextErrorMessage7 {get; set;}

    
    private String baseUrl = 'https://www.google.com/recaptcha/api/siteverify';
    private String secret = '6LfhchEUAAAAAHB6OuvyEp093srAR0qkpu1vx-Yl';
    
    public String sitekey 
    {
        get{return '6LfhchEUAAAAADq1zE8wGqviN92b2IemvHSEmvuK';}
    }
    
    public String response 
    { 
        get { return ApexPages.currentPage().getParameters().get('g-recaptcha-response'); }
    }
    
    
    public PageReference submit() 
    {
        String responseBody = makeRequest(baseUrl,
                'secret=' + secret +
                '&response='+ response
        );
        String success = getValueFromJson(responseBody, 'success');
        if(success.equalsIgnoreCase('true')){

       System.debug('Entered into save');
       if(con.FirstName == null)
       {inputTextErrorMessage='Please enter "First Name"';
       }
       if(con.LastName__c == null)
       {inputTextErrorMessage2='Please enter "Last Name"';
       }
       if(con.Country_Code__c == 'CountryCode*' || con.ContactPhone__c == null)
       
       {inputTextErrorMessage4='Please choose "Country Code" & enter "Mobile Number"';
       }
       if(con.Email == null)
       {inputTextErrorMessage5='Please enter "Email"';
       }
       if(con.Nationality__c == 'Nationality*')
       {inputTextErrorMessage6='Please choose "Nationality"';
       }
       if(con.Preferred_Time_to_Call__c == 'Preferred time to Call* (GMT +4)')
       {inputTextErrorMessage7='Please choose "Preferred time to Call"';
       return null;}
            
con.LastName = con.LastName__c  ;     
string conname = con.FirstName+' '+con.LastName__c;
system.debug('conname====>'+conname );
integer flag =1;
list<contact> conlist = [select name,email from contact ];
for(contact c:conlist){
if(c.name == conname && c.email == con.email){
pba__Request__c r = new pba__Request__c();
r.pba__Contact__c = c.id;
r.status__c = 'Pending';

try{
    list<pba__Request__c> relist = [select id,pba__Contact__c,status__c, createddate from pba__Request__c];
         for(pba__Request__c req:relist)
         {
                if(r.pba__Contact__c == req.pba__Contact__c)
                {
                   if((req.status__c == 'Active' || req.status__c == 'Pending')&& (req.createddate.date().daysbetween(System.today()))<7)
                   {
                          pagereference pg = new pagereference('https://c.cs86.visual.force.com/apex/OmniyatErrMsg');
                          return pg;
                          return null;
       
       }
    
    }
    }
    }
    catch(DMLException e) 
        {
          return null;
        }  
insert r;
flag++;
}
}
system.debug('flag==='+flag);
if(flag==1)
{

insert con;
pba__Request__c r = new pba__Request__c();
r.pba__Contact__c = con.id;
r.status__c = 'Active';
insert r;

}


pagereference pg = new pagereference('http://omniyat-omniyat.cs86.force.com/thankyou');
return pg;
       
        }
        
        else{
        inputTextErrorMessage3='Please verify Captcha';
        return null;
        }
        
    }
    
    
    private String makeRequest(string url, string body)  {
        HttpResponse response = null;
        HttpRequest req = new HttpRequest();   
        req.setEndpoint(url);
        req.setMethod('POST');
        req.setBody (body);
        
        try {
            Http http = new Http();
            response = http.send(req);
            return response.getBody();
            }
            catch(System.Exception e)
            {
            System.debug('ERROR: ' + e);
            }
        return '{"success":false}';
    }   
    
    
    public string getValueFromJson ( String strJson, String field ){
        JSONParser parser = JSON.createParser(strJson);
        while (parser.nextToken() != null) {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)) {
                if(parser.getText() == field){
                    // Get the value.
                    parser.nextToken();
                    return parser.getText();
                }
            }
        }
        return null;
    }

}
Test Class: 49% coverage
 
@isTest
public class FormController_Test{
public static testmethod void FormController_Test1()

{
Test.StartTest();

PageReference pageRef = new pagereference('https://cs86.visual.force.com/apex/OmniyatErrMsg');
Test.setCurrentPage(pageRef);

PageReference pageRef1 = new pagereference('http://omniyat-omniyat.cs86.force.com/thankyou');
Test.setCurrentPage(pageRef1);

Contact conTemp = new Contact();

conTemp.FirstName = 'Full';
conTemp.LastName__c = 'Name';
conTemp.LastName = conTemp.LastName__c;
conTemp.Nationality__c = 'India';
conTemp.Preferred_time_to_call__c = 'Forenoon';
conTemp.Country_Code__c = 'Afghanistan +93';
conTemp.Email = 'test@test.com';
conTemp.Phone = '9191919191';

FormController contrl = new FormController();
contrl.Con = conTemp;
contrl.submit();

pba__Request__c reqTemp1 = new pba__Request__c();

reqTemp1.pba__Contact__c = conTemp.Id;
reqTemp1.Status__c = 'Closed';

insert reqTemp1;

Contact conTemp1 = new Contact();

conTemp1.FirstName = 'Full';
conTemp1.LastName__c = 'Name';
conTemp1.Nationality__c = 'India';
conTemp1.Preferred_time_to_call__c = 'Forenoon';
conTemp1.Country_Code__c = 'Afghanistan +93';
conTemp1.Email = 'test@test.com';
conTemp1.Phone = '9191919191';
conTemp1.LastName = conTemp1.LastName__c;

ApexPages.StandardController sc = new ApexPages.StandardController(conTemp1);
FormController ControllerObj = new FormController(sc);
ControllerObj.inputTextErrorMessage = 'test';
System.assertEquals(ControllerObj.inputTextErrorMessage , 'test');
ControllerObj.inputTextErrorMessage2 = 'test';
System.assertEquals(ControllerObj.inputTextErrorMessage2 , 'test');
ControllerObj.inputTextErrorMessage3 = 'test';
System.assertEquals(ControllerObj.inputTextErrorMessage3 , 'test');
ControllerObj.inputTextErrorMessage4 = 'test';
System.assertEquals(ControllerObj.inputTextErrorMessage4 , 'test');
ControllerObj.inputTextErrorMessage5 = 'test';
System.assertEquals(ControllerObj.inputTextErrorMessage5 , 'test');
ControllerObj.inputTextErrorMessage6 = 'test';
System.assertEquals(ControllerObj.inputTextErrorMessage6 , 'test');
ControllerObj.inputTextErrorMessage7 = 'Forenoon';
System.assertEquals(ControllerObj.inputTextErrorMessage7 , 'Forenoon');
ControllerObj.FirstName = 'test';
System.assertEquals(ControllerObj.FirstName , 'test');
ControllerObj.LastName= 'test';
System.assertEquals(ControllerObj.LastName, 'test');
ControllerObj.Email= 'test@test.com';
System.assertEquals(ControllerObj.Email, 'test@test.com');
ControllerObj.Phone= '1111';
System.assertEquals(ControllerObj.Phone, '1111');




ControllerObj.submit();


pba__Request__c reqTemp2 = new pba__Request__c();

reqTemp2.pba__Contact__c = conTemp.Id;
reqTemp2.Status__c = 'Active';

insert reqTemp2;


Test.StopTest();

 }

  }

Thanks,
Avinash