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
angusgrantangusgrant 

Test Method help. 63% test coverage thus far.

Im having problems with my apex test method how can i test the following line highligted in red I know the test method is getting into the if statment. i guess what i need to do is test for the existance of a new account? How is this done? I also then need to test the population of this new (database) object account with data? pls advise.

 

 

I dont know if its just me but I found the test mehod documentation very poor. On the apex official documentation site it gives examples of test methods without giving the code that the test methods actually relates too. Pls wake up Salesforce there needs to be better documentation vote for idea

 

thanks for your time

 

Angus

AlsoDougAlsoDoug

I am pretty sure that the only reason that line would not be run is if the if statement (picklistvalue == 0) is not executed (can always drop a debug statement in there to verify).

 

I have certainlly created new accounts in testing and been able to test the line of code.

 

 

angusgrantangusgrant

Hi

 

Thanks for your message it might be that I am setting the picklistvalue var after I have set the page

 

 e.g.

 

   

ApexPages.standardController controller = new ApexPages.standardController(Contact); appExtension busextension = new appExtension(controller); busextension.setpicklistValue('0');

 

 

 

 

How do I populate picklistvalue var before the page is loaded? My terminology maybe wrong. I think this is the case because i can tell picklistvalue is populated in save method as it gets into the if statment.

AlsoDougAlsoDoug

Terminology can change from language to language but I think I am following what you're saying.

 

If the code you posted was all in the constructor of the class, IE public ClassName() or something like that, then setting the picklist value after you have created the class in your testing class won't do anything because the code has already run.

 

If this is true you generally have a couple of options:

 

1. If you test coverage is fine, you can just not worry about it (probably not best practice).

 

2. Move code out of your constructor into sub methods which you can call in your constructor in your normal code and after you have created the main object and set the value in your testing class.

 

I have come across you're issue before and generally do 2.

 

I also frequently find that when I am writing the test coverage that I will end up rewriting my main code.

 

My rule of thumb (by no means a universal truth) is that if it's hard to write the tests for my code, I probably need to look at how I am laying out the class I am testing.

 

Hope that's of some help.

 

Doug

 

BritishBoyinDCBritishBoyinDC

I think you are on the right track. If you've created a new instance of the controller and set the picklist value to 0 in in the test, then I think you can just call the function, and it should test that line

 

i.e. controller.newAccount();