I have write this class in Salesforce but not able write test class. Please anybody can help me to write test class.

This is my controller class:

public class AccountRelatedContacts  {
        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 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)); 
                        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;

could you please tell me writing test code.

1 Answer 1


In your second test testgetCountryNames you call:


method immediately before your set the country ID value in the next line with:

controllers.selectedcouId = c.id;

Change the order of these statements.

Consistent indenting would make it easier for you and others to understand your code.

  • 1
    +1 for the gentle admonition on indenting; Improving the world of software development, one OP at a time :-)
    – cropredy
    Feb 11, 2016 at 16:16
  • Hi I changed order of there two stateemnts again it will show error only code not covering,country id showing null
    – user123
    Feb 12, 2016 at 5:47
  • can any one help on this
    – user123
    Feb 12, 2016 at 6:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .