+ Start a Discussion
S SaiS Sai 

how Write Test class

HI how to write test class for this class 

public class CountryAccountRelatedContacts  {
        public id tobeEdited{get; set;}
        public String selectedcouId{get;set;}
        public String selectedAccId{get;set;}
        public string conid{get;set;}
        public boolean showPopup{ get; set; }
         public boolean isDownload {get;set;}
        
        public list<contact> lstcontact{get;set;}
        
        public PageReference Download(){
    isDownload = true;
      return null;
    }
        
         public List<SelectOption> getCountryNames() {
        
                 List<SelectOption> couOptions= new List<SelectOption>();
                          couOptions.add( new SelectOption('','--Select--'));
                          list<Country__c> cut = [select Id,name from Country__c];
                          for( Country__c  cou : cut ) {
                                  couOptions.add( new SelectOption(cou.Id,cou.name));
                          }
                         return couOptions;
                   }
           
   
        
               public List<SelectOption> getAccountNames() {
               system.debug('country ID'+selectedcouid);
                  List<SelectOption> accOptions= new List<SelectOption>();
                  List<SelectOption> options = new List<SelectOption>();
                  if(selectedcouId != null){
                  
                  list<account> act = [select Id,name,Country__c from account where Country__c=:selectedcouId order by name ASC ];
                  
                  for( Account acc : act  ) {
                          accOptions.add( new SelectOption(acc.Id,acc.name)); 
                  }
                  }
               else
                    {
                        accOptions.add( new SelectOption('--None--','--None--'));
                    }
             return accOptions;
       }
       
       
      public pagereference Submit(){
      
      lstcontact= new List<contact>();
      lstcontact= [SELECT ID,name, Firstname,lastname,email,phone,account.name from contact WHERE accountId=:selectedaccid order by name ASC];
      return null;
    }
    
    public PageReference openpopup() {
        showPopup =true;
        return null; 
    }
    public PageReference cancel() {
        showPopup =false;
        return null; 
    }
    public PageReference DeleteCon() {
        List<contact> con = [select id,name,Phone,accountid from contact where Id= :conid];
        delete con;
        showPopup =false;
       // contacts =new List<contact>();
        return null; 
    }
    
    
         public String getTobeEdited() {
        return null;
    }
    
     public PageReference Editable() {
        return null;
    }
    
    
    
    
  
    public void saveRecord(){
        Contact tobeupdated;
        for(Contact temp : lstcontact){
            if(temp.id==tobeEdited){
                tobeupdated = temp;   
                break;
            }
        }
        update tobeupdated;
        tobeEdited = null;
    }
}

Thanks 
SS
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about test classes in salesforce
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Please try below code
@isTest 
public class ExtensionTestClass 
{
	static testMethod void testMethod1() 
	{
		Country__c cou = new Country__c();
		cou.name ='US';
		// add all required field here
		insert cou;

		Country__c cou1 = new Country__c();
		cou1.name ='IND';
		// add all required field here
		insert cou1;
		
		
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		testAccount.Country__c = cou.id;
		insert testAccount;

		Contact cont= new Contact();
		cont.FirstName= 'demo';
		cont.LastName ='Demo';
		cont.accountid =testAccount.id;
		insert cont;
		
		Test.StartTest(); 

			CountryAccountRelatedContacts   testAccPlan = new CountryAccountRelatedContacts();
			testAccPlan.selectedcouId = cou.id;
			testAccPlan.accountId = testAccount.id;
			testAccPlan.conid = cont.id;
			
			List<SelectOption> litCont = testAccPlan.getCountryNames();		
			List<SelectOption> litAcc = testAccPlan.getAccountNames();	
			testAccPlan.openpopup();
			testAccPlan.cancel();
			testAccPlan.Download();
			
			testAccPlan.Submit();
			testAccPlan.Editable();
			testAccPlan.saveRecord();
			testAccPlan.DeleteCon();

		Test.StopTest();
	}
}
Please let us know if this will help you

 
S SaiS Sai
HI Amit,

Thanks for your reply it's throughing error when i am saving   like 

[Error] Error: Compile Error: Method does not exist or incorrect signature: Test.StartTest() at line 28 column 9

Thanks 
SS
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
 
@isTest 
public class ExtensionTestClass 
{
	static testMethod void testMethod1() 
	{
		Country__c cou = new Country__c();
		cou.name ='US';
		// add all required field here
		insert cou;

		Country__c cou1 = new Country__c();
		cou1.name ='IND';
		// add all required field here
		insert cou1;
		
		
		Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		testAccount.Country__c = cou.id;
		insert testAccount;

		Contact cont= new Contact();
		cont.FirstName= 'demo';
		cont.LastName ='Demo';
		cont.accountid =testAccount.id;
		insert cont;
		
			CountryAccountRelatedContacts testAccPlan = new CountryAccountRelatedContacts();
			testAccPlan.selectedcouId = cou.id;
			testAccPlan.accountId = testAccount.id;
			testAccPlan.conid = cont.id;
			
			List<SelectOption> litCont = testAccPlan.getCountryNames();		
			List<SelectOption> litAcc = testAccPlan.getAccountNames();	
			testAccPlan.openpopup();
			testAccPlan.cancel();
			testAccPlan.Download();
			
			testAccPlan.Submit();
			testAccPlan.Editable();
			testAccPlan.saveRecord();
			testAccPlan.DeleteCon();
	}
}
Please let us know if this will help you
 
S SaiS Sai
HI Amit

Its Giveing 0%

Thanks 
SS
S SaiS Sai
Hi Amit
Not Working
Thanks
SS
Amit Chaudhary 8Amit Chaudhary 8
Can you please post your test class and let us know what error you are getting