+ Start a Discussion
rupesh ranjanrupesh ranjan 

I want to write Test class

I want to write Test class for belowclass
public class Contactpage{
 public Contactpage(ApexPages.StandardController controller) {
    }
public List<contactDemoMailSentwrap> ConsoleWrapperList4{get;set;}
public List<contactDemoMailSentwrap> getperformcallout4(){
ConsoleWrapperList4 = new List<contactDemoMailSentwrap>();

HttpRequest req = new HttpRequest();

HttpResponse res = new HttpResponse();

Http http = new Http();
req.setEndpoint('https://www.demomail.net/sf/api/ContactDemoMailSent?Email=twilms@verisign.con&NameSpace=Demos%20On%20Demand%20Report'); 

req.setMethod('GET');
req.setTimeout(10000);
res = http.send(req);
//System.assert(false,res.getBody()+'-----');
if(res.getstatusCode() == 200 && res.getbody() != null){
String replaceIllegal= res.getbody().replaceAll('\n','').replaceAll('\r','');
ConsoleWrapperList4=(List<contactDemoMailSentwrap>)System.JSON.deserialize(replaceIllegal,List<contactDemoMailSentwrap>.class);

}

return consolewrapperlist4;
}
}

 
Sai Kalyan SanisettySai Kalyan Sanisetty
Hi Rupesh,

For the Above Apex class we need to create the Webservice Mock Class,

Please find the below code for more information,

Test Class 

@isTest
public class ContactpageTest
{
    static testmethod void Myunittest1()
    {
        // Your Standard object instance
        Standardobject a = new standardobject();
        insert a;
        Contactpage cp = new Contactpage(new ApexPages.StandardController(a));
        Test.setMock(HttpCalloutMock.class, new ContactpageTestmock());
        HttpResponse res = Contactpage.getperformcallout4();
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
        cp.getperformcallout4();
    
    }

}

Mock Class

@IsTest 
global class ContactpageTestmock implements HttpCalloutMock 
{
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
        System.assertEquals('https://www.demomail.net/sf/api/ContactDemoMailSent?Email=twilms@verisign.con&NameSpace=Demos%20On%20Demand%20Report', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"foo":"bar"}');
        res.setStatusCode(200);
        return res;
    }
}

 
rupesh ranjanrupesh ranjan
I have an error in Test class Method does not exist or incorrect signature: homeReport.getperformcallout()

Class-------
public with sharing class homeReport{
private ApexPages.StandardController c;
public homeReport(ApexPages.StandardController stdController) {
        c = stdController;
        }
    public   string d='eU9WzoFgU4n8Apu5PYxcNGRZswRDZJWDEMdbQVU85gw=';
     public   String Acode= Userinfo.getuserid();
     public   String Oid = UserInfo.getOrganizationId();
    Blob cryptoKey= EncodingUtil.base64Decode(d);
   
    Blob data4 = Blob.valueOf(Acode);
    Blob data5 = Blob.valueOf(Oid);
    
    Blob encryptedData4 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data4 );
    Blob encryptedData5 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data5 );
 
    public    String b64Data4 = EncodingUtil.base64Encode(encryptedData4);
    public    String b64Data5 = EncodingUtil.base64Encode(encryptedData5);
    //String testurl='sessionID='+EncodingUtil.urlEncode(b64Data, 'UTF-8')+'&serverUrl='+EncodingUtil.urlEncode(b64Data1, 'UTF-8')+'&contactID='+EncodingUtil.urlEncode(b64Data2, 'UTF-8')+'&email='+EncodingUtil.urlEncode(b64Data3, 'UTF-8');
      
    public String aucode= EncodingUtil.urlEncode(b64Data4, 'UTF-8');
    public String ocode= EncodingUtil.urlEncode(b64Data5, 'UTF-8');


  public List<homeReportwrap> ConsoleWrapperList{get;set;}
  
       public List<homeReportwrap> getperformcallout(){
      ConsoleWrapperList = new List<homeReportwrap>();
       HttpRequest req = new HttpRequest(); 
       HttpResponse res = new HttpResponse();
        Http http = new Http(); 
        string url = 'https://www.demomail.net/sf/api/HomeVideoViewReport?AUCode='+aucode+'&SOrgID='+ocode;
        //System.debug('##: '+ url );
      req.setEndpoint(url);
      //s req.setEndpoint('https://www.demomail.net/sf/api/HomeVideoViewReport?AUCode=aucode&SOrgID=ocode'); 
 //req.setEndpoint('https://www.demomail.net/sf/api/HomeVideoViewReport?AUCode=04uZru9MNwFgaEdddAUOHL0Y%2FtNoWSrhDMQpMyLHbVMBBENHjxc%2B7XO1Tl4T5rA4&SOrgID=IievdI%2Fz5GQAPQEWAzN7cc45a4rUCoRqbEad1KON7DSUK%2F1pxBNzk3%2FB1JcpahR2');
        req.setMethod('GET');
        system.debug('###'+d);
         system.debug('???'+Acode);
           system.debug('***'+Oid );
         
        
        //req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
        res = http.send(req);
         //System.assert(false,res.getBody()+'******');
       
         if(res.getstatusCode() == 200 && res.getbody() != null)
          { 
        String replaceIllegal= res.getbody().replaceAll('\n','').replaceAll('\r','');
        ConsoleWrapperList=(List<homeReportwrap>)System.JSON.deserialize(replaceIllegal,List<homeReportwrap>.class);
        //ConsoleWrapperList1 = ConsoleWrapperList.replaceall('\r\n','');
          //System.debug('Response Checking Engine: '+ res.getBody());
          }
            return consolewrapperlist; 
             
          }
               
 
 }
homepageTestmock
@IsTest 
global class homepageTestmock implements HttpCalloutMock 
{
    global HTTPResponse respond(HTTPRequest req) {
        
            string d='eU9WzoFgU4n8Apu5PYxcNGRZswRDZJWDEMdbQVU85gw=';
      String Acode= Userinfo.getuserid();
        String Oid = UserInfo.getOrganizationId();
    Blob cryptoKey= EncodingUtil.base64Decode(d);
   
    Blob data4 = Blob.valueOf(Acode);
    Blob data5 = Blob.valueOf(Oid);
    
    Blob encryptedData4 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data4 );
    Blob encryptedData5 = Crypto.encryptWithManagedIV('AES256', cryptoKey, data5 );
 
       String b64Data4 = EncodingUtil.base64Encode(encryptedData4);
       String b64Data5 = EncodingUtil.base64Encode(encryptedData5);
     
     String aucode= EncodingUtil.urlEncode(b64Data4, 'UTF-8');
    String ocode= EncodingUtil.urlEncode(b64Data5, 'UTF-8');
   
        System.assertEquals('https://www.demomail.net/sf/api/HomeVideoViewReport?AUCode='+aucode+'&SOrgID='+ocode, req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"foo":"bar"}');
        res.setStatusCode(200);
        return res;
    }
}
Test Class
@isTest
public class homeReportTest
{
    static testmethod void Myunittest1()
    {
        // Your Standard object instance
        User a = new User();
        insert a;
        homeReport cp = new homeReport(new ApexPages.StandardController(a));
        Test.setMock(HttpCalloutMock.class, new homepageTestmock());
        HttpResponse res = homeReport.getperformcallout();
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"foo":"bar"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());
        cp.getperformcallout();
        res = http.send(req);
    
    }

}