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

Salesforce Apex code coverage



I searched a lot of way to improve my code and so far it was working fine.
(around 6-7 classes)


I'm currently fightinh with  Strange class class, the validator stop with IF statement or Object's really crazy...


I wrote this post to ask to the community if any one got issues with the code coverage avout :

- IF statement

- Statement inside a loop ( IF inside a For ..)

- Items creation...


i found some idea on internet but i just improved my test from 10 to 23 % ...






trystatements try thinking of it this way - 

you don't write the unit tests to get code coverage, instead you do that to check that the code behaves the way you expected.


if your unit test doesn't cover the inside of an if statement, than you probably didn't write a test scenario in which the answer to that IF is true.


for example, if I had an insert trigger with a for on the collection and an if inside that FOR loop,like:


for(contact c :


      If( == 'test')

          //do something



in order to would the //do something line, I would create a unit test that goes something like:

contact con = new contact();

con.lastName = 'test';


insert con;



this way I:


1.created data

2.Got inside a for loop

3.Got into the IF block


hope this helps


very helpfull!

But now i'm in front of an other case.


i have to test an Apex Pages :i don't know how ceate it in my apex test class, here an example


if(InStock(oppProds) == true){
			ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Some product are not in stock please check it'));
			return null;
		else {
// working code