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
Mats ErikssonMats Eriksson 

Testing of getter;setter

I have problems testing my getter/setter. I'm rather new to it and am missing the nuances.

 

I have a class that looks like this:

  

public with sharing class clsCheckDuplicateSerials {
	public string CheckDuplicateSerials{get; set;}
	  String srchText;
	  	  
	  public String getSearchText() {
	    return srchText;
	  }
	  public void setSearchText(String s) {
	    srchText = s;
	  }
}

 The test method looks like this:

@isTest
private class TestDuplicateISerials {
    static testMethod void myUnitTest() {
        String sText ='ABX9043092058';
        CheckDuplicateSerials mm= new CheckDuplicateSerials();
        mm.getSearchText =sText;
        String s = mm.getSearchText ;
        systemAssert(sText,s)
    }
}

 

This doesn't work, I get "Variable does not exist: getSearchText". I have tried different methods of assignment but only the error messages change.

 

It's the test class that fails, the first one doesn't throw any errors.

 

I'm aware of the issue with execution order of the getter and setter but ignoring it for the moment, just want to be able to get the testing going.

 

Any advice?

 

/Mats

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

There's a couple of things that jump out at me:

 

(1) Your class is called clsCheckDuplicateSerials, but in the test class you instantiate CheckDuplicateSerials  - is that a cust/paste error?  otherwise I wouldn't expect it to save.

 

(2) get/setSearchText are methods, but you are trying to use them like properties. I think you want something like:

 

   mm.setSearchText(sText);
   String s = mm.getSearchText();
   systemAssert(sText,s)

 

All Answers

bob_buzzardbob_buzzard

There's a couple of things that jump out at me:

 

(1) Your class is called clsCheckDuplicateSerials, but in the test class you instantiate CheckDuplicateSerials  - is that a cust/paste error?  otherwise I wouldn't expect it to save.

 

(2) get/setSearchText are methods, but you are trying to use them like properties. I think you want something like:

 

   mm.setSearchText(sText);
   String s = mm.getSearchText();
   systemAssert(sText,s)

 

This was selected as the best answer
Mats ErikssonMats Eriksson

Thanks for your answer.

 

Yes, the class name was a typo when composing the posting on this site.

 

However, I actually tried before posting with calling the methods like you describe but came across: TestDuplicateSerials: Method does not exist or incorrect signature: [CheckDuplicateSerials].GetSearchText(String)

 

Another typo somewhere?

 

/Mats

bob_buzzardbob_buzzard

This error:

 

: [CheckDuplicateSerials].GetSearchText(String)

 

Implies you have done something like:

 

String s = mm.getSearchText(text1);

 I.e. you have tried to call the getSearchText with a string parameter, but there is no method with that signature.

Mats ErikssonMats Eriksson

Gack!

 

Another typo....

 

Thank you Bob for your help!

 

 

sa_nasa_na

@istest
private class clsCheckDuplicateSerials_test
{
static testmethod void check()
{
clsCheckDuplicateSerials nm=new clsCheckDuplicateSerials();
string sText='ABX9043092058';
    nm.getSearchText();  
    nm.setSearchText(sText);

    
   }
}

this should help i think:)

AK2017AK2017
Hi Bob,

I am expereincing same situation but i am not able to cover the get method.

below is the snippet,
 
public class PhdAppsProgressViewcontroller {

   
    public ApexPages.StandardSetController con {get; set;}
    public boolean searchPerformed{get;set;}
    public Integer queryLimit { get{return 50000;}}
    public class PhdOppDocuments {
    
        public String OppName {get;Set;}
        public String ProgramName {get;Set;}        
        public Boolean Resume {get; set;}
        public Boolean Recommendation {get; set;}
        public Boolean WritingSample {get; set;}
        public Boolean unoffiTranscripts {get; set;}
        public Boolean CoverLetter {get; set;}        
        public String CurrentStatus {get;set;}
        public String OppID {get;set;} 
        public String Owner {get;set;}
        public Integer Age {get;set;}   
        public String Term {get;set;}   
        
    }

       
     /*Public Methods */
    public List<PhdOppDocuments> lstOppDocuments { 
      
       get{
            //System.debug('***con.getResultSize()'+con.getResultSize());
         if(con != null)   {
            List<PhdOppDocuments> lstPhdOppDocuments = new List<PhdOppDocuments>();
            for (Opportunity Opp: (List<Opportunity>)con.getRecords()){
              PhdOppDocuments oppdoc = new PhdOppDocuments();
              Datetime LOR1;
              Datetime LOR2;
              Date LastModifieddate;
              for(Attachment__c att:Opp.Attachments__r){
                  If(att.Document_Type__c == 'Resume/CV'){
                      oppdoc.Resume = true;
                  }                  
                  
                  If(att.Document_Type__c == 'Letter of Recommendation' && LOR1 == null){
                      LOR1 = att.CreatedDate;                     
                  }
                  
                   If(att.Document_Type__c == 'Letter of Recommendation' && LOR1 != null && LOR1 != att.CreatedDate){
                      LOR2 = att.CreatedDate;
                      oppdoc.Recommendation = true;
                  }
                  
                 
              oppdoc.CurrentStatus = Opp.Opportunity_Status__c;
              oppdoc.OppID = Opp.ID;
              oppdoc.OppName = Opp.Name; 
              oppdoc.Owner= Opp.Owner.Name;
              oppdoc.ProgramName = opp.Program_Name__c; 
              oppdoc.Term = Opp.term__r.Name;             
              lstPhdOppDocuments.add(oppdoc); 
            }             
              return lstPhdOppDocuments; 
            } else  
                return new List<PhdOppDocuments>();   
        } 
        set;
        
        
    }  
   


}

below is my test method.
 
PhdAppsProgressViewcontroller  repPOC = new PhdAppsProgressViewcontroller();
         system.debug('submitted date source'+system.today());
        repPOC.Field = 'Term__r.Name';
        repPOC.Fieldvalue = 'Fall 2014';
        pref = repPOC.runreport();
        List<PhdOppDocuments> lstOppDocuments = repPOC.getlstOppDocuments();      
        repPOC.next();
        repPOC.previous();

I am getting error message as invalid type PhdOppDocuments. PhdOppDocuments is  a class defined in the controller class.

Please help mw covering this class.

Aariff