function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PSahuPSahu 

how to write test class of a class in which value in method get by current page url

I am new in apex and salesforce please tell me how to write test class of a class in which value in method get by current page url my class is given below please reply me

global with sharing class multiweatherController {  
     
     public Multi_Weather__c data{get; set;}

   
    public String[] contract;
    public string contractid;

    private ApexPages.StandardController ctrl;        

     public multiweatherController() {       
            
         List<Multi_Weather__c> mwList = [
           SELECT Id FROM Multi_Weather__c];
        
        if(mwList.size()!=0)
        {   
        multi_weather = [SELECT Id, name, Width__c, City_Code__c, Weather_Theme__c, Theme_Background_Color__c, Metro_Text_color__c, Show_Next_Days__c, Temp_Unit__c, High_Low_Temp__c, Wind_Speed__c, Humidity__c, Show_Country__c, Visibility__c, Show_Sunrise__c, Show_Sunset__c FROM Multi_Weather__c];
        ApexPages.currentPage().getParameters().put('Id', multi_weather.id);
        }
      
          
              }
       
        
         public string getcontactid()
        {
           //leadid = apexpages.currentpage().getparameters().get('sfdcIFrameOrigin');   
           contactid = ApexPages.currentPage().getHeaders().get('referer');
            
           contact = contactid.split('/');
           
            try {
            
            List<Contact> contactdata = [Select Id, MailingCity from Contact Where Id =:contact[3]];
                        
              return contactdata.get(0).MailingCity;       
              
            }
            Catch (exception e) {
           
              return null;
            }       
          
        }
              
       

}

Thanks in advance
Prady01Prady01
https://developer.salesforce.com/forums/ForumsMain?id=906F000000090DlIAI

Thanks
Prady01
PSahuPSahu
Please check it i am unable to find solution :(

global with sharing class multiweatherController {    
     public Multi_Weather__c data{get; set;}
    public boolean detailsinfo;
    public boolean editform;
    public boolean full_metro_render;
    public boolean metro_render;
    public Multi_Weather__c MW;
    public Multi_Weather__c multi_weather{get;set;}
    public string Wk_weather_theme;
    public String Wk_Weather_Theme_Class;
    public String[] Wk_City_code;
    public string result = '';
    public string[] Wk_city;
    public string leadid;
    public String[] lead;
    public string accountid;
    public String[] account;
    public string contactid;
    public String[] contract;
    public string contractid;
    public String[] contact;
     public multiweatherController() {                  
         List<Multi_Weather__c> mwList = [
           SELECT Id FROM Multi_Weather__c];       
        if(mwList.size()!=0)
        {   
        multi_weather = [SELECT Id, name, Width__c, City_Code__c, Weather_Theme__c, Theme_Background_Color__c, Metro_Text_color__c, Show_Next_Days__c, Temp_Unit__c, High_Low_Temp__c, Wind_Speed__c, Humidity__c, Show_Country__c, Visibility__c, Show_Sunrise__c, Show_Sunset__c FROM Multi_Weather__c];
        ApexPages.currentPage().getParameters().put('Id', multi_weather.id);
        }          
              }             
           public string getcontractid()
        {
           //leadid = apexpages.currentpage().getparameters().get('sfdcIFrameOrigin');   
           contractid = ApexPages.currentPage().getHeaders().get('referer');           
           contract = contractid.split('/');          
            try {           
            List<Contract> contractdata = [Select Id, BillingCity from Contract Where Id =:contract[3]];                       
              return contractdata.get(0).BillingCity;                    
            }
            Catch (exception e) {          
              return null;            }                
        }       
         public string getcontactid()
        {
           //leadid = apexpages.currentpage().getparameters().get('sfdcIFrameOrigin');   
           contactid = ApexPages.currentPage().getHeaders().get('referer');           
           contact = contactid.split('/');          
            try {           
            List<Contact> contactdata = [Select Id, MailingCity from Contact Where Id =:contact[3]];                       
              return contactdata.get(0).MailingCity;                  
            }
            Catch (exception e) {          
              return null;
            }                
        }             
         public string getaccountid()
        {
           //leadid = apexpages.currentpage().getparameters().get('sfdcIFrameOrigin');   
           accountid = ApexPages.currentPage().getHeaders().get('referer');           
           account = accountid.split('/');          
            try {           
            List<Account> accountdata = [Select Id, BillingCity from Account Where Id =:account[3]];                       
              return accountdata.get(0).BillingCity;       
              
            }
            Catch (exception e) {
           
              return null;
            }       
          
        }           
              
        public string getleadid()
        {
           //leadid = apexpages.currentpage().getparameters().get('sfdcIFrameOrigin');   
           leadid = ApexPages.currentPage().getHeaders().get('referer');
            
           lead = leadid.split('/');
           
            try {
            
            List<Lead> leaddata = [Select Id, City from Lead Where Id =:lead[3]];
                        
              return leaddata.get(0).City;       
              
            }
            Catch (exception e) {
           
              return null;
            }       
          
        }     
              
         public string getWoeid()
            {
            
            if(getleadid() != null)
            {
                Http http =new Http();
                HttpRequest req =new HttpRequest();
                  req.setEndpoint('http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%281%29%20where%20text=%22'+getleadid()+',%20il%22&format=json');
                req.setmethod('GET');
                HttpResponse res = http.send(req);
                String str = res.getbody();
                Woeid_JSON2Woeid_Apex wk_woeid = Woeid_JSON2Woeid_Apex.parse(str);   
            
                result  = '"'+wk_woeid.query.Results.Place.woeid +'"';
            } else if(getaccountid() != null)
            {
                Http http =new Http();
                HttpRequest req =new HttpRequest();
                  req.setEndpoint('http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%281%29%20where%20text=%22'+getaccountid()+',%20il%22&format=json');
                req.setmethod('GET');
                HttpResponse res = http.send(req);
                String str = res.getbody();
                Woeid_JSON2Woeid_Apex wk_woeid = Woeid_JSON2Woeid_Apex.parse(str);
                result  = '"'+wk_woeid.query.Results.Place.woeid +'"';  
            
            } else if(getcontactid() != null)
            {
                 Http http =new Http();
                HttpRequest req =new HttpRequest();
                  req.setEndpoint('http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%281%29%20where%20text=%22'+getcontactid()+',%20il%22&format=json');
                req.setmethod('GET');
                HttpResponse res = http.send(req);
                String str = res.getbody();
                Woeid_JSON2Woeid_Apex wk_woeid = Woeid_JSON2Woeid_Apex.parse(str);
                result  = '"'+wk_woeid.query.Results.Place.woeid +'"';  
            
            } else if(getcontractid() !=null)
            {
                  Http http =new Http();
                HttpRequest req =new HttpRequest();
                  req.setEndpoint('http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%281%29%20where%20text=%22'+getcontractid()+',%20il%22&format=json');
                req.setmethod('GET');
                HttpResponse res = http.send(req);
                String str = res.getbody();
                Woeid_JSON2Woeid_Apex wk_woeid = Woeid_JSON2Woeid_Apex.parse(str);
                result  = '"'+wk_woeid.query.Results.Place.woeid +'"';  
            }
            else {
                List<String> Wk_city = multi_weather.City_Code__c.split(',');   
                
            for(integer i=0; i<Wk_city.size(); i++){
                
                Http http =new Http();
                HttpRequest req =new HttpRequest();
 
                 req.setEndpoint('http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%281%29%20where%20text=%22'+Wk_city[i]+',%20il%22&format=json');
             
               
                req.setmethod('GET');
                HttpResponse res = http.send(req);
                String str = res.getbody();
                Woeid_JSON2Woeid_Apex wk_woeid = Woeid_JSON2Woeid_Apex.parse(str);   
            
                result  = '"'+wk_woeid.query.Results.Place.woeid +'"'+ ',' + result;
                
                }
                 //result = Wk_city[i] + i + result;
                
            }                        
              return result;                    
            }     
              
         public boolean getfull_metro_render(){

            if(multi_weather.Weather_Theme__c == 'Full Metro')
             {
                 //metro_render = true;
                 full_metro_render = true;
                
             } else
             {
                   //metro_render = false;
                 full_metro_render = false;
             }
            return full_metro_render;
            }     
              
        public boolean getmetro_render(){
         if(multi_weather.Weather_Theme__c != 'Full Metro')
         {
             metro_render = true;
             //full_metro_render = true;
            
         } else
         {
               metro_render = false;
            // full_metro_render = false;
         }
        return metro_render;
        }   
        
        public string getWk_Country_code(){

        if (multi_weather.Weather_Theme__c == 'Metro' || multi_weather.Weather_Theme__c == 'Full Metro') {
            Wk_City_code = getWoeid().split(',');
            return Wk_City_code[0];    
        } else {
        
            return getWoeid();
        
        }          

        }   
        
        public string getWk_Tempunit(){

        if(multi_weather.Temp_Unit__c == 'Celsisus')
        {
            multi_weather.Temp_Unit__c = 'c';
        } else
        {
            multi_weather.Temp_Unit__c = 'f';
        }
        return multi_weather.Temp_Unit__c;
        }
        
        public string getWk_weather_func(){

         if(multi_weather.Weather_Theme__c == 'Metro')
         {
             
             Wk_weather_theme = 'weatherfeedmetro';
         } else
         {
             Wk_weather_theme = 'weatherfeed';
         }
            return Wk_weather_theme;
        }
        
        public string getWk_Weather_Theme(){

        if (multi_weather.Weather_Theme__c == 'Dark') {
            Wk_Weather_Theme_Class = 'vertical_dark';
        } else if (multi_weather.Weather_Theme__c == 'Full Metro') {
            Wk_Weather_Theme_Class = 'vertical_full_metro';
        } else if (multi_weather.Weather_Theme__c == 'Light') {
            Wk_Weather_Theme_Class = 'vertical_light';
        } else if (multi_weather.Weather_Theme__c == 'Metro') {
            Wk_Weather_Theme_Class = 'vertical_metro';
        } else if (multi_weather.Weather_Theme__c == 'Simple Dark') {
            Wk_Weather_Theme_Class = 'vertical_simple_dark';
        } else if (multi_weather.Weather_Theme__c == 'Simple Light') {
            Wk_Weather_Theme_Class = 'vertical_simple_light';
        }
        
            return multi_weather.Weather_Theme__c;
        }
        
        public string getWk_main_content(){
 
         string Weather_Theme = getWk_Weather_Theme();
         string content;
         
         if(multi_weather.Weather_Theme__c == 'Metro' || multi_weather.Weather_Theme__c == 'Full Metro')
         {
              content = '<div id="wkweath_item" class="'+Wk_Weather_Theme_Class+'"; style="height:auto; display:inline-block; width:'+multi_weather.Width__c+'px; background: none repeat scroll 0 0 '+multi_weather.Theme_Background_Color__c+' " > </div>';
          
         } else
         {
              
            content = '<div style="width:'+multi_weather.Width__c+'px;"><div class="wk_scroll" ><div id="wkweath_item" class="'+Wk_Weather_Theme_Class+' items"; style="width:'+multi_weather.Width__c+'px;"></div></div><div class="navi"></div></div>';
         }
         
            return content;
        }
        

}
PSahuPSahu

and test class is

@isTest(SeeAllData=true)
private class multiweatherControllerTest {    
    @isTest static void testcontractid() {   
    test.startTest();   
    contract contract;     
    //string url = ApexPages.currentPage().getParameters().put('referer', 'https://ap2.salesforce.com/80028000000LK88');
        multiweatherController mvc = new multiweatherController();
        mvc.getcontractid();                              
      //string obj= multiweatherController.getcontractid('https://ap2.salesforce.com/80028000000LK88');
          //System.assertEquals(obj, 'chennai');       
                                                   
        //multiweatherController mwc = new multiweatherController();
       // mwc.getcontractid();  
       // System.assertEquals(obj, true);    
         
    }
    public static string getcontactid(string contactids)
        {         
            String[] contact = contactids.split('/');              
            List<Contact> contactdata = [Select Id, MailingCity from Contact Where Id =:contact[3]];                       
              return contactdata.get(0).MailingCity;          
        }

    @isTest static void testcontactid() {    
    String contactid = 'https://ap2.salesforce.com/00328000005AyTy';
        string obj = getcontactid(contactid);
        System.assertEquals(obj, 'delhi');           
    }  
      public static string getaccountid(string accountids)
        {         
            String[] account = accountids.split('/');              
            List<Account> accountdata = [Select Id, BillingCity from Account Where Id =:account[3]];                       
              return accountdata.get(0).BillingCity;          
        }
    @isTest static void testaccountid() {    
    String accountid = 'https://ap2.salesforce.com/00128000005X0tr';
        string obj = getaccountid(accountid);
        System.assertEquals(obj, 'paris');           
    }    
    public static string getleadid(string leadids)
        {         
            String[] lead = leadids.split('/');              
              List<Lead> leaddata = [Select Id, City from Lead Where Id =:lead[3]];                      
              return leaddata.get(0).City;        
        }

    @isTest static void testleadid() {    
    String leadid = 'https://ap2.salesforce.com/00Q280000029bsB';
        string obj = getleadid(leadid);
        System.assertEquals(obj, 'delhi');           
    }   
     public static string getWoeid(string Woeid_data)
        {                  
                /*Http http =new Http();
                HttpRequest req =new HttpRequest();
                  req.setEndpoint('http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%281%29%20where%20text=%22'+Woeid_data+',%20il%22&format=json');
                req.setmethod('GET');
                HttpResponse res = http.send(req);
                String str = res.getbody();
                Woeid_JSON2Woeid_Apex wk_woeid = Woeid_JSON2Woeid_Apex.parse(str);            
                string result  = wk_woeid.query.Results.Place.woeid; */                     
              return '2390809';          
        }

    @isTest static void testWoeid() {    
    String Woeid = 'delhi';
        string obj = getWoeid(Woeid);
        System.assertEquals(obj, '2390809');           
    }
    
     public static boolean getfull_metro_render(string full_metro_render_data)
        {       
            boolean info;
              
           if(full_metro_render_data == 'Full Metro')
             {
                 //metro_render = true;
                 info = true;
                
             } else
             {
                   //metro_render = false;
                 info = false;
             }
            return info;
            }              
       

    @isTest static void testfull_metro_render() {    
    String full_metro_render = 'Full Metro';
        boolean obj = getfull_metro_render(full_metro_render);
        System.assertEquals(obj, true);           
    }
    
     public static boolean getmetro_render(string metro_render_data)
        {       
            boolean info;
              
           if(metro_render_data == 'Metro')
             {
                 //metro_render = true;
                 info = true;
                
             } else
             {
                   //metro_render = false;
                 info = false;
             }
            return info;
            }         
    @isTest static void testmetro_render() {    
    String metro_render = 'Metro';
        boolean obj = getmetro_render(metro_render);
        System.assertEquals(obj, true);           
    }
    
      public static string getWk_Tempunit(string Tempunit){

        if(Tempunit == 'Celsisus')
        {
           Tempunit = 'c';
        } else
        {
            Tempunit = 'f';
        }
        return Tempunit;
        }
        
       @isTest static void testWk_Tempunit() {    
        String unit = 'Celsisus';
        string obj = getWk_Tempunit(unit);
        System.assertEquals(obj, 'c');           
    }
    
      public static string getWk_weather_func(string Weather_Theme){
         if(Weather_Theme == 'Metro')
         {            
             weather_theme = 'weatherfeedmetro';
         } else
         {
             weather_theme = 'weatherfeed';
         }
            return weather_theme;
        }
        
        @isTest static void testWk_weather_func() {    
        String theme = 'Metro';
        string obj = getWk_weather_func(theme);
        System.assertEquals(obj, 'weatherfeedmetro');           
        }
        
        public static string getWk_Weather_Theme(string wk_theme){
        string Wk_Weather_Theme_Class;
        if (wk_theme == 'Dark') {
            Wk_Weather_Theme_Class = 'vertical_dark';
        } else if (wk_theme == 'Full Metro') {
            Wk_Weather_Theme_Class = 'vertical_full_metro';
        } else if (wk_theme == 'Light') {
            Wk_Weather_Theme_Class = 'vertical_light';
        } else if (wk_theme == 'Metro') {
            Wk_Weather_Theme_Class = 'vertical_metro';
        } else if (wk_theme == 'Simple Dark') {
            Wk_Weather_Theme_Class = 'vertical_simple_dark';
        } else if (wk_theme == 'Simple Light') {
            Wk_Weather_Theme_Class = 'vertical_simple_light';
        }       
            return Wk_Weather_Theme_Class;
        }
        
         @isTest static void testWk_Weather_Theme() {    
        String theme = 'Dark';
        string obj = getWk_Weather_Theme(theme);
        System.assertEquals(obj, 'vertical_dark');           
        }           
        public static string getWk_main_content(string weather_theme){
         string Wk_Weather_Theme_Class = getWk_Weather_Theme(weather_theme);
         string content;        
         if(weather_theme == 'Metro' || weather_theme == 'Full Metro')
         {
              content = '<div id="wkweath_item" class="'+Wk_Weather_Theme_Class+'"; style="height:auto; display:inline-block; width:170px; background: none repeat scroll 0 0 white " > </div>';
          
         } else
         {            
            content = '<div style="width:170px;"><div class="wk_scroll" ><div id="wkweath_item" class="'+Wk_Weather_Theme_Class+' items"; style="width:170px;"></div></div><div class="navi"></div></div>';
         }        
            return content;
        }       
         @isTest static void testWk_main_content() {    
        String theme = 'Dark';
        string obj = getWk_main_content(theme);
        System.assertEquals(obj, '<div style="width:170px;"><div class="wk_scroll" ><div id="wkweath_item" class="vertical_dark items"; style="width:170px;"></div></div><div class="navi"></div></div>');           
        }       
         public static string getWk_Country_code(string wk_theme){     
            return 'delhi';
        }       
         @isTest static void testWk_Country_code() {    
        String theme = 'Dark';
        string obj = getWk_Weather_Theme(theme);
        System.assertEquals(obj, 'vertical_dark');           
        }
}