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
Linga_RaminLinga_Ramin 

Unable to cover below Try catch block in Test class.Below is my method

public string amtStrData(string rstr){
        string strg = rstr;
        if(rstr.contains(' " ')){
            try{
            integer index1= rstr.indexOf('"')+1;
            string p1= rstr.substring(index1);
            integer index2= p1.indexOf('"')+index1;         
            string amount = rowstr.substring(index1,index2);
            amount = amount.replace(',','');
            strg = rstr.substring(0,index1-1)+amount+rstr.substring(index2+1);
            }catch(Exception e){}
        }
Ramesh DRamesh D
Replace this line  if(rstr.contains(' " ')) to if(rstr.contains('"')) 
or if you dont wanna remove spaces
when you call this methond in your test class pass string which contains spaces like this amtStrData(' " teststring " ');

I hope you find the above solution helpful. If it does mark as Best Answer to help others too.
Thanks,
Ramesh D
Deepali KulshresthaDeepali Kulshrestha
Hi PM ,

try this
 
@isTest public class amtStrData{
    @isTest public static void amtStrDataTest()
    {
        String strg = '';
        Test.StartTest(); 
            yourclassname.amtStrData(strg);
            
        Test.StopTest();
    }
}

and i don't understand "rstr.contains(' " ')" if you are checking null value of string please correct it.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

 
Ajay K DubediAjay K Dubedi
Hi,
I have gone through your code and everything seems to work fine except  if(rstr.contains(' " ')) due to which control doesn't gets into try block (here I am believing you are passing the string having ". Coming to problem, I don't think its a proper way to check " try using if(rstr.contains(' \" ')) instead of if(rstr.contains(' " ')).

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi