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
jordanmjordanm 

Test Case for passing parameters via button click

Hello,

 

I have a custom wizard controller that is working and am trying to write a test case for it. I have a button that writes three parameters to querystring in the url.

 

Controller

public class osmController {

   // These member variables maintain the state of the wizard. 
   // When users enter data into the wizard, their input is stored 
   // in these variables.  
    
   Participant_Assessment__c pAsmt;
   Participant_Interaction__c pInt;
   Participant_Answer__c pAns1;
 ...
   private final Participant__c participant;
   private final Assessment__c assessment;
   private final Family__c family;
   string possibleanswer1 = null;
 ...

   // The next methods return one of each of the member 
   // variables. If this is the first time the method is called, 
   // it creates an empty record for the variable. 
      
   public List<selectOption> getanswer1() {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options       
        for (Possible_Answer__c pa1 : [SELECT Id, Answer_Display__c FROM Possible_Answer__c
         WHERE Dimension__c = 'Income']) { 
            options.add(new selectOption(pa1.Id, pa1.Answer_Display__c)); //for all records found - add them to the picklist options
        }
        return options; //return the picklist options
 ...
   public List<selectOption> getInteractiontypes() {
        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options        
        options.add(new selectOption('', '--None--')); //add the first option of '- None -' in case the user doesn't want to select a value or in case no values are returned from query below
        for (Interaction_Type__c Interactiontypes : [SELECT Id, Description__c FROM Interaction_Type__c WHERE IsDeleted = FALSE]) { 
            options.add(new selectOption(Interactiontypes.Description__c, Interactiontypes.Description__c)); //for all records found - add them to the picklist options
        }
        return options; //return the picklist options
    }
   
   public Participant_Assessment__c getpAsmt() {
      if(pAsmt == null) pAsmt = new Participant_Assessment__c();
      return pAsmt;
   }
   
   public Participant_Interaction__c getpInt() {
      if(pInt == null) pInt = new Participant_Interaction__c();
      return pInt;
   }
   
   public Participant_Answer__c  getpAns1() {
      if(pAns1 == null) pAns1 = new Participant_Answer__c();
      return pAns1;
   }
   public Participant_Answer__c  getpAns2() {
      if(pAns2 == null) pAns2 = new Participant_Answer__c();
      return pAns2;
   }
   public Participant_Answer__c  getpAns3() {
      if(pAns3 == null) pAns3 = new Participant_Answer__c();
      return pAns3;
   }
   public Participant_Answer__c  getpAns4() {
      if(pAns4 == null) pAns4 = new Participant_Answer__c();
      return pAns4;
   }
   public Participant_Answer__c  getpAns5() {
      if(pAns5 == null) pAns5 = new Participant_Answer__c();
      return pAns5;
   }
   public Participant_Answer__c  getpAns6() {
      if(pAns6 == null) pAns6 = new Participant_Answer__c();
      return pAns6;
   }
   public Participant_Answer__c  getpAns7() {
      if(pAns7 == null) pAns7 = new Participant_Answer__c();
      return pAns7;
   }
   public Participant_Answer__c  getpAns8() {
      if(pAns8 == null) pAns8 = new Participant_Answer__c();
      return pAns8;
   }
   public Participant_Answer__c  getpAns9() {
      if(pAns9 == null) pAns9 = new Participant_Answer__c();
      return pAns9;
   }
   public Participant_Answer__c  getpAns10() {
      if(pAns10 == null) pAns10 = new Participant_Answer__c();
      return pAns10;
   }
   public Participant_Answer__c  getpAns11() {
      if(pAns11 == null) pAns11 = new Participant_Answer__c();
      return pAns11;
   }
   public Participant_Answer__c  getpAns12() {
      if(pAns12 == null) pAns12 = new Participant_Answer__c();
      return pAns12;
   }
   public Participant_Answer__c  getpAns13() {
      if(pAns13 == null) pAns13 = new Participant_Answer__c();
      return pAns13;
   }
   public Participant_Answer__c  getpAns14() {
      if(pAns14 == null) pAns14 = new Participant_Answer__c();
      return pAns14;
   }
   public Participant_Answer__c  getpAns15() {
      if(pAns15 == null) pAns15 = new Participant_Answer__c();
      return pAns15;
   }
   public Participant_Answer__c  getpAns16() {
      if(pAns16 == null) pAns16 = new Participant_Answer__c();
      return pAns16;
   }
   public Participant_Answer__c  getpAns17() {
      if(pAns17 == null) pAns17 = new Participant_Answer__c();
      return pAns17;
   }
   public Participant_Answer__c  getpAns18() {
      if(pAns18 == null) pAns18 = new Participant_Answer__c();
      return pAns18;
   }
   
   public Participant__c getParticipant() {
            return participant;
   }
   
   public Assessment__c getAssessment() {
            return assessment;
   } 
   
   public Family__c getFamily() {
            return family;
   }   
   
   public osmController() {

            if (ApexPages.currentPage().getParameters().get('family') == '') {
            	//do nothing
            } else {
            	participant = [select id, name from Participant__c where id =  <================GETTING ERROR HERE
                       :ApexPages.currentPage().getParameters().get('id')];
            }
            assessment = [select id, name from Assessment__c where Name = 
                       :ApexPages.currentPage().getParameters().get('asmt') AND
                       Version__c = :ApexPages.currentPage().getParameters().get('version')];
                       
            if (ApexPages.currentPage().getParameters().get('family') == '') {
            	//do nothing
            } else {
            	family = [select id, name, Poverty_Level_2012__c from Family__c where name = 
                                         :ApexPages.currentPage().getParameters().get('family')];
            }              
   }
    
   public String getpossibleanswer1() {
            return possibleanswer1;
   }   
   public String getpossibleanswer2() {
            return possibleanswer2;
   }
   public String getpossibleanswer3() {
            return possibleanswer3;
   }   
   public String getpossibleanswer4() {
            return possibleanswer4;
   }   
   public String getpossibleanswer5() {
            return possibleanswer5;
   }   
   public String getpossibleanswer6() {
            return possibleanswer6;
   }   
   public String getpossibleanswer7() {
            return possibleanswer7;
   }   
   public String getpossibleanswer8() {
            return possibleanswer8;
   }   
   public String getpossibleanswer9() {
            return possibleanswer9;
   }   
   public String getpossibleanswer10() {
            return possibleanswer10;
   }   
   public String getpossibleanswer11() {
            return possibleanswer11;
   }   
   public String getpossibleanswer12() {
            return possibleanswer12;
   }   
   public String getpossibleanswer13() {
            return possibleanswer13;
   }   
   public String getpossibleanswer14() {
            return possibleanswer14;
   }   
   public String getpossibleanswer15() {
            return possibleanswer15;
   }   
   public String getpossibleanswer16() {
            return possibleanswer16;
   }   
   public String getpossibleanswer17() {
            return possibleanswer17;
   }   
   public String getpossibleanswer18() {
            return possibleanswer18;
   }      
   
   public void setpossibleanswer1(String possibleanswer1) { this.possibleanswer1 = possibleanswer1; }
   public void setpossibleanswer2(String possibleanswer2) { this.possibleanswer2 = possibleanswer2; }
   public void setpossibleanswer3(String possibleanswer3) { this.possibleanswer3 = possibleanswer3; }
   public void setpossibleanswer4(String possibleanswer4) { this.possibleanswer4 = possibleanswer4; }
   public void setpossibleanswer5(String possibleanswer5) { this.possibleanswer5 = possibleanswer5; }
   public void setpossibleanswer6(String possibleanswer6) { this.possibleanswer6 = possibleanswer6; }
   public void setpossibleanswer7(String possibleanswer7) { this.possibleanswer7 = possibleanswer7; }
   public void setpossibleanswer8(String possibleanswer8) { this.possibleanswer8 = possibleanswer8; }
   public void setpossibleanswer9(String possibleanswer9) { this.possibleanswer9 = possibleanswer9; }
   public void setpossibleanswer10(String possibleanswer10) { this.possibleanswer10 = possibleanswer10; }
   public void setpossibleanswer11(String possibleanswer11) { this.possibleanswer11 = possibleanswer11; }
   public void setpossibleanswer12(String possibleanswer12) { this.possibleanswer12 = possibleanswer12; }
   public void setpossibleanswer13(String possibleanswer13) { this.possibleanswer13 = possibleanswer13; }
   public void setpossibleanswer14(String possibleanswer14) { this.possibleanswer14 = possibleanswer14; }
   public void setpossibleanswer15(String possibleanswer15) { this.possibleanswer15 = possibleanswer15; }
   public void setpossibleanswer16(String possibleanswer16) { this.possibleanswer16 = possibleanswer16; }
   public void setpossibleanswer17(String possibleanswer17) { this.possibleanswer17 = possibleanswer17; }
   public void setpossibleanswer18(String possibleanswer18) { this.possibleanswer18 = possibleanswer18; }
   


   // The next methods control navigation through 
   // the wizard. Each returns a PageReference for one of the pages 
   // in the wizard. Note that the redirect attribute does not need to 
   // be set on the PageReference because the URL does not need to change 
   // when users move from page to page. 
    
   public PageReference step1() {
      return Page.osmStep1;
   }
   public PageReference step2() {
      return Page.osmStep2;
   }
   public PageReference step3() {
      return Page.osmStep3;
   }
   public PageReference step4() {
      return Page.osmStep4;
   }
   public PageReference step5() {
      return Page.osmStep5;
   }
   public PageReference step6() {
      return Page.osmStep6;
   }
   public PageReference step7() {
      return Page.osmStep7;
   }
   public PageReference step8() {
      return Page.osmStep8;
   }
   public PageReference step9() {
      return Page.osmStep9;
   }
   
   // This method cancels the wizard, and returns the user to the  
   // Participants tab 
    
    public PageReference cancel() {
            PageReference participantPage = new ApexPages.StandardController(participant).view();
            participantPage.setRedirect(true);
            return participantPage; 
    }
    
   // This method performs the final save for all objects, and 
   // then navigates the user to the detail page for the new 
   // participant assessment. 
    
   public PageReference save() {
          PageReference p = null;
          
          if (this.family == null || this.participant == null || this.assessment == null) {
          	p = Page.failure;
          	p.getParameters().put('error','noParam');
          } else {
          	try {
          		// Create the participant assessment. Before inserting, use the id field 
          // from the active participant and assessment that are passed via custom button to create 
          // the relationship between the participant and assessment and the participant assessment. 
        
          pAsmt.Participant__c = participant.id;
          pAsmt.Assessment__c = assessment.id;
          insert pAsmt;
    
          // Create the participant answers. Before inserting, use the id field 
          // that's created once the participant assessment is inserted to create 
          // the relationship between the participant assessment and the participant answer. 
          
          pAns1.Participant_Assessment__c = pAsmt.id;
          pAns1.Possible_Answer__c = possibleanswer1;
          insert pAns1;
          pAns2.Participant_Assessment__c = pAsmt.id;
          pAns2.Possible_Answer__c = possibleanswer2;
          insert pAns2;
          pAns3.Participant_Assessment__c = pAsmt.id;
          pAns3.Possible_Answer__c = possibleanswer3;
          insert pAns3;
          pAns4.Participant_Assessment__c = pAsmt.id;
          pAns4.Possible_Answer__c = possibleanswer4;
          insert pAns4;
          pAns5.Participant_Assessment__c = pAsmt.id;
          pAns5.Possible_Answer__c = possibleanswer5;
          insert pAns5;
          pAns6.Participant_Assessment__c = pAsmt.id;
          pAns6.Possible_Answer__c = possibleanswer6;
          insert pAns6;
          pAns7.Participant_Assessment__c = pAsmt.id;
          pAns7.Possible_Answer__c = possibleanswer7;
          insert pAns7;
          pAns8.Participant_Assessment__c = pAsmt.id;
          pAns8.Possible_Answer__c = possibleanswer8;
          insert pAns8;
          pAns9.Participant_Assessment__c = pAsmt.id;
          pAns9.Possible_Answer__c = possibleanswer9;
          insert pAns9;
          pAns10.Participant_Assessment__c = pAsmt.id;
          pAns10.Possible_Answer__c = possibleanswer10;
          insert pAns10;
          pAns11.Participant_Assessment__c = pAsmt.id;
          pAns11.Possible_Answer__c = possibleanswer11;
          insert pAns11;
          pAns12.Participant_Assessment__c = pAsmt.id;
          pAns12.Possible_Answer__c = possibleanswer12;
          insert pAns12;
          pAns13.Participant_Assessment__c = pAsmt.id;
          pAns13.Possible_Answer__c = possibleanswer13;
          insert pAns13;
          pAns14.Participant_Assessment__c = pAsmt.id;
          pAns14.Possible_Answer__c = possibleanswer14;
          insert pAns14;
          pAns15.Participant_Assessment__c = pAsmt.id;
          pAns15.Possible_Answer__c = possibleanswer15;
          insert pAns15;
          pAns16.Participant_Assessment__c = pAsmt.id;
          pAns16.Possible_Answer__c = possibleanswer16;
          insert pAns16;
          pAns17.Participant_Assessment__c = pAsmt.id;
          pAns17.Possible_Answer__c = possibleanswer17;
          insert pAns17;
          pAns18.Participant_Assessment__c = pAsmt.id;
          pAns18.Possible_Answer__c = possibleanswer18;
          insert pAns18;
          
          //create participant interaction for OSM completion
          pInt.Interaction_Date__c = pAsmt.Assessment_Date__c;
          pInt.Interaction_Summary__c = 'OSM Completed';
          pInt.Participant__c = participant.id;
          insert pInt;
          	} catch (Exception e) {
          		p = Page.failure;
          		p.getParameters().put('error','noInsert');
          	}
          }
          
          if (p == null) {
          	p = new ApexPages.StandardController(pAsmt).view();
          }
          p.setRedirect(true);
          return p;
      }
}

 

The test method, which tells me the controller is hitting exception: "List has no rows for assignment to sObject" on line 307 which I marked in the above controller code...

 

@isTest
public class TestosmController {
	
    public static testMethod void myUnitTest(){
        
        PageReference pageRef1 = Page.osmStep1;
        Test.setCurrentPage(pageRef1);
        
        osmController controller = new osmController();
        String savePage = controller.save().getUrl();
        
        // verify that page fails without parameters
        System.assertEquals('/apex/failure?error=noParam', savePage);
        
        
        Profile pf = [Select Id from Profile where Name = 'Sys Admin'];

        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'America/Mexico_City';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;

        system.runAs(u){

            Family__c o = new Family__c();
            o.Family_Type__c = 'Other';
            insert o;

            system.assertEquals(o.Family_Income__c, null);

            //Test payments on insert
            Participant__c p1 = new Participant__c();
            p1.Family__c = o.Id;
            p1.First_Name__c = 'Testy';
            p1.Last_Name__c = 'Testoferson';
            p1.Social_Security_Number__c = '999999998';
            p1.Date_of_Birth__c = Date.newInstance(2000, 1, 1);
            p1.Disabled__c = 'No';
            p1.Education__c = 'Unknown';
            p1.Gender__c = 'Unknown';
            p1.Ethnicity__c = 'No Response';
            p1.Race__c = 'African American';
            p1.Primary_Language__c = 'Spanish';
            p1.Health_Insurance__c = 'Yes';
            p1.SSN_Status__c = 'Verified';
            p1.Food_Stamps__c = 'No';
            p1.Veteran_Code__c = 'No';
            p1.WIC__c = 'No';
            p1.Marital_Status__c = 'Single';
            insert p1;

        // Add parameters to page URL 
        ApexPages.currentPage().getParameters().put('id', p1.id);
                       
        }
    }
}

 I'm trying to insert the id of a freshly inserted participant into the query string parameter in my test method and it is still returning null, any idea what I'm doing wrong?

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You don't appear to be placing the id into the URL parameters until after the controller has been instantiated by your test class - its too late then, as the constructor is executing the query that is erroring.  

All Answers

bob_buzzardbob_buzzard

You don't appear to be placing the id into the URL parameters until after the controller has been instantiated by your test class - its too late then, as the constructor is executing the query that is erroring.  

This was selected as the best answer
jordanmjordanm

D'oh! That was it! Thank you

jordanmjordanm

Drats! I changed the test class to this...

 

It picks up the id, asmt, and version parameters just fine, but fails to pick up the family name field...?

 

any way I can get more information about how/why this is occurring?

 

@isTest
public class TestosmController {
	
    public static testMethod void myUnitTest(){
        
        PageReference pageRef1 = Page.osmStep1;
        Test.setCurrentPage(pageRef1);
        PageReference pageRef2 = Page.osmStep2;
        Test.setCurrentPage(pageRef2);
        PageReference pageRef3 = Page.osmStep3;
        Test.setCurrentPage(pageRef3);
        PageReference pageRef4 = Page.osmStep4;
        Test.setCurrentPage(pageRef4);
        PageReference pageRef5 = Page.osmStep5;
        Test.setCurrentPage(pageRef5);
        PageReference pageRef6 = Page.osmStep6;
        Test.setCurrentPage(pageRef6);
        PageReference pageRef7 = Page.osmStep7;
        Test.setCurrentPage(pageRef7);
        PageReference pageRef8 = Page.osmStep8;
        Test.setCurrentPage(pageRef8);
        PageReference pageRef9 = Page.osmStep9;
        Test.setCurrentPage(pageRef9);

        Profile pf = [Select Id from Profile where Name = 'Sys Admin'];

        User u = new User();
        u.FirstName = 'Test';
        u.LastName = 'User';
        u.Email = 'testuser@test123456789.com';
        u.CompanyName = 'test.com';
        u.Title = 'Test User';
        u.Username = 'testuser@test123456789.com';
        u.Alias = 'testuser';
        u.CommunityNickname = 'Test User';
        u.TimeZoneSidKey = 'America/Mexico_City';
        u.LocaleSidKey = 'en_US';
        u.EmailEncodingKey = 'ISO-8859-1';
        u.ProfileId = pf.Id;
        u.LanguageLocaleKey = 'en_US';
        insert u;

        system.runAs(u){
			//insert family
            Family__c fa = new Family__c();
            fa.Family_Type__c = 'Other';
            insert fa;

            //insert participant
            Participant__c p1 = new Participant__c();
            p1.Family__c = fa.id;
            p1.First_Name__c = 'Testy';
            p1.Last_Name__c = 'Testoferson';
            p1.Social_Security_Number__c = '999999998';
            p1.Date_of_Birth__c = Date.newInstance(2000, 1, 1);
            p1.Disabled__c = 'No';
            p1.Education__c = 'Unknown';
            p1.Gender__c = 'Unknown';
            p1.Ethnicity__c = 'No Response';
            p1.Race__c = 'African American';
            p1.Primary_Language__c = 'Spanish';
            p1.Health_Insurance__c = 'Yes';
            p1.SSN_Status__c = 'Verified';
            p1.Food_Stamps__c = 'No';
            p1.Veteran_Code__c = 'No';
            p1.WIC__c = 'No';
            p1.Marital_Status__c = 'Single';
            insert p1;
            
            //insert OSM
            Assessment__c a1 = new Assessment__c();
            a1.Name = 'Outcome Scale Matrix';
            a1.Version__c = '2,0';
            insert a1;

            // Add parameters to page URL 
            ApexPages.currentPage().getParameters().put('fam', fa.Name);
            ApexPages.currentPage().getParameters().put('id', p1.id);
            ApexPages.currentPage().getParameters().put('asmt', a1.Name);
            ApexPages.currentPage().getParameters().put('version', a1.Version__c);
            
            osmController controller = new osmController();
        	String savePage = controller.save().getUrl();
        
        	// verify that page fails without parameters
        	System.assertEquals('/apex/failure?error=noParam', savePage);
        }
    }
}

 

bob_buzzardbob_buzzard

fa.name is probably null when you add it to the parameters, as while you have inserted the Family__c record, you haven't queried it back from the database to populate the name field.

jordanmjordanm

You can't fetch the Name field like you can the id field of the inserted record?

bob_buzzardbob_buzzard

No, the ID is a special one that gets populated by the platform. Everything else about your record will be as you left it.

jordanmjordanm

Gotcha, thanks again for the help!

ssrssr

Hi I need test calss for the below calss 

 

iam getting error is that list has no rows for assign ment object.

public class GE_MCS_ChangeOwner 
{
    public String CaseId {get;set;}
    public String UserId {get;set;}
    public String Action {get;set;}
    PageReference caseUrl;

    
    public GE_MCS_ChangeOwner()
    {
        Action = ApexPages.currentPage().getParameters().get('Action');       
        CaseId = ApexPages.currentPage().getParameters().get('CaseId');  
        caseUrl = new PageReference('/'+CaseId);  
        system.debug('lakshmi' + CaseId); 
    }
    
    Public PageReference reDirect() 
    {
        If(Action == 'Accept') return ChangeOwnerName();
    
        caseUrl.setRedirect(True);        
        return caseUrl ; 
        
    }
    
    Public PageReference ChangeOwnerName() 
    {
        Case CaseId = [select OwnerId from case where id =:CaseId];
        System.debug('Lakshmi1' + caseId.OwnerId);
        System.debug('Lakshmi2' + UserInfo.getUserId()); 
        CaseId.Ownerid = UserInfo.getUserId();  
        update CaseId;
        return caseUrl;
    }
}

 

@isTest
public class GE_MCS_ChangeOwner_Test
{
  public string caseId{get;set;}
  static testmethod void GE_MCS_ChangeOwner()
  {
       Id ProfileID = [ SELECT Id FROM Profile WHERE name = 'system Administrator'].Id;
       // Id Roles =[ SELECT Id FROM Userrole WHERE name = 'Application Administrator'].Id; 
        User aUser = new User( email='test-user@fakeemail.com',title='cems',profileid = profileid, UserName='test-user@fakeemail.com', alias='tuser1', CommunityNickName='tuser1',TimeZoneSidKey='America/New_York', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1',LanguageLocaleKey='en_US', FirstName = 'Test', LastName = 'User');
        insert aUser; 
        System.debug('Lakshmi2' + UserInfo.getUserId());
      
           Case csobj=new Case();
            csobj.Subject='Email';
            //csobj.Status='Closed';
            csobj.Origin='Email';
           // csobj.ownerId = aUser.id;
            csobj.ownerId = UserInfo.getUserId();
            insert csobj;
            
            System.debug('Lakshmi1' + csobj.OwnerId);
            PageReference prActual = Page.ChangeOwner;  
       prActual.getParameters().put('Id',csobj.id);
       ApexPages.StandardController sController1 = new ApexPages.StandardController(csobj); 
            
           
  
     GE_MCS_ChangeOwner co = new GE_MCS_ChangeOwner();
     pagereference pr = co.reDirect();
     csobj.ownerId = UserInfo.getUserId();
     
     pagereference pre = co.ChangeOwnerName();
 
  }
  
}

 Please help me on this

Thanks in adavance