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
RiPaulRiPaul 

Totally Confused on how to create a unit test for getters and setters

Hi folks,

 

How in the world do I test my getters / setters in this class snippet?

 

public with sharing class VFControllerSendEmail {

     

      // Nice explanation on how these property getters and setters actaully work

      // http://blog.sforce.com/sforce/2008/06/property-access.html

      public String email {get {return email;} set {email = email;}}

     

...

...

 

I then have a unit test that does the following in an attempt to test the get/set email prop.

 

...

...

VFControllerSendEmail sendFollowUp = new VFControllerSendEmail(theID);

System.assert(sendFollowUp != null);

sendFollowUp.email = 'sommer@jnbridge.com';

String testEm = sendFollowUp.email;

System.assert(sendFollowUp.email != null);

System.debug('TEST 3 email: ' + testEm);

 

...

...

 

Now, when I run the Apex Test Runner, my Code Coverage Results tell me that the get/set email props are not covered.   More specifically, I get "Line 5, Column 28 not covered".    What?   What I'm I missing??

 

Thanks in advance for any help one of you might be able to extend.

 

Best regards,

Paul

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Its not that you are invoking it incorrectly, when you provide a body for a setter, there's an implicit parameter called "value" that is passed to it.  Your code isn't using this parameter, but is simply doing:

 

 

email = email;

This applies the current value of email to email, not the value that your unit test is trying to put in there. 

 

 

Looking at your code again, you don't actually need a body for the getter/setter, as all you are doing is returning or updating the email variable.

 

If you change your code to:

 

 

public String email {get; set;}

 

it will still work and the unit test will cover it.

 

All Answers

bob_buzzardbob_buzzard

Your setter is simply assigning the current value of email to itself.

 

From the Apex docs:

 

--- snip ---

 

When the set accessor is invoked, the system passes an implicit argument to the setter called value

 of the same data type as the property.

 

--- snip ---

 

Thus you need:

 

 

public String email {get {return email;} set {email = value;}}

 

 

RiPaulRiPaul

Hi Bob,

 

Thanks very much for the reply.   I'm not understanding your reply, however.   I have the suggested prop definition in my apex class, the "public String email {get {return email;} set {email = email;}}".   Are you saying I'm invoking it incorrectly from my unit test? 

Apologies in advance for the learning curve confusion.

Best regards and thanks again,

-P

bob_buzzardbob_buzzard

Its not that you are invoking it incorrectly, when you provide a body for a setter, there's an implicit parameter called "value" that is passed to it.  Your code isn't using this parameter, but is simply doing:

 

 

email = email;

This applies the current value of email to email, not the value that your unit test is trying to put in there. 

 

 

Looking at your code again, you don't actually need a body for the getter/setter, as all you are doing is returning or updating the email variable.

 

If you change your code to:

 

 

public String email {get; set;}

 

it will still work and the unit test will cover it.

 

This was selected as the best answer
RiPaulRiPaul

Ah ha!  The light just went on.  Thanks very-very much!   Now, onto testing the whole class.   Should be interesting....

 

Thanks again,

-P 

RiPaulRiPaul

Hi Bob,

 

I'm afraid I'm stuck again.   Consider the following snippit:

 

...

...

 

try {

            rec = [select id, name, email from Contact where Id =:t.whoID];

            if (rec != null) {

                  this.name = rec.name;

                  System.debug('REC Name =' + rec.name);

                  // Will need to have a check for Lead vs. Contact.

                  this.email = rec.email;  

                  System.debug('REC Email =' + rec.email);

       

                  send(name, email);

            }

        }

        catch(Exception ex)

         {

                  System.debug('Paul Caught Exception = ' + ex);

         }

...

...

 

Now the Code Coverage Results are telling me that the following lines from above are not covered.   What gives??

 

 if (rec != null) {  // NOT COVERED

                  this.name = rec.name;  // !NOT COVERED

                  System.debug('REC Name =' + rec.name);

                  // Will need to have a check for Lead vs. Contact.

                  this.email = rec.email;    // NOT COVERED

                  System.debug('REC Email =' + rec.email);

       

                  send(name, email);  // NOT COVERED

Do I need to write more code on the unit test side to exercise this logic and variable assignments??   I'm having a fundamental confusion about where all the validation checks should go -- in the class definition itself vs. the unit test code.  

Thanks again in advance for any new light you can extend to me as I grapple with this [new to me] development paradigm.

-P

 

bob_buzzardbob_buzzard

That's exactly what you need to do.  It looks like you aren't setting up any test data that would cause the code to enter the body of the if statement (i.e. when rec!=null).

 

The debug statements don't count for code coverage, so they will never be marked as not covered, which can be confusing.

RiPaulRiPaul

Very good!  I'm passing my unit tests now.  

 

Thanks very-very much for the support -- greatly apprecaited.

-P