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
xNamelessonexxNamelessonex 

SOQL Code coverage in Test Classes

I have read some posts on the forum, which use this as a demonstration of how to create good code coverage. http://amitsalesforce.blogspot.mx/2015/06/best-practice-for-test-classes-sample.html


I am using the Force.com Developer console, and when running tests the code coverage goes up to 40% or less, sometimes changing by 2%. The code not covered, according to the console has the following format:


 
if(Database.countQuery('SELECT count() FROM User WHERE User.Id = \''+ Object.variable+'\'') > 0)
             usr.add(Database.query('SELECT Id,Email FROM User WHERE User.Id = \''+ Object.variable +'\''));
Now how do have to code this from the test perspective? I have created SObjects accordingly in the test class, but even if I do, the console does not increase the code coverage for the class intended to be set in production.


 
Best Answer chosen by xNamelessonex
FearNoneFearNone
hi xNamelessonex,

if you don't want to create instance of SObject, change your SObject methods to Static.
sample code here:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

Good Luck!

All Answers

FearNoneFearNone
hi xNamelessonex,

try to post your whole code and test code, and let's discuss from there...
xNamelessonexxNamelessonex
My code has the following format now increasing the code coverage to 93% under a sandbox environment:

 
@isTest(seeAllData = true)

//The see all data option is used because creating data from scratch would cause insert errors
//This was used as a last resort
//this is all pseudocode based on the original code
public class TestClass{



 static testMethod void testFunction() {

 Test.startTest();



List<CaseComment> cslist = new List<CaseComment>(); 


CaseCommentCopy cscopy = new CaseCommentCopy(); //Instance of the class meant to be tested

cslist = Database.query('SELECT ParentId, CommentBody FROM CaseComment LIMIT 3');


cscopy.CommentToEmail(cslist); 

//The CommentToEmail Function is the member function of the class which has all the functionality mentioned before, the database queries, which needed to be covered 

Test.stopTest();










     }



}

However when deploying it to production it still claims the code coverage is below the 75% required. Does anyone know of any way of doing unit tests with SOQL queries that do not require creating instances of every single SObject intended to be tested? 
FearNoneFearNone
hi xNamelessonex,

if you don't want to create instance of SObject, change your SObject methods to Static.
sample code here:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

Good Luck!
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Can you please post the Apex class and your test class so that we can help you
 
xNamelessonexxNamelessonex
Thank you for all the help. I have now understood where I was mistaken. 
FearNoneFearNone
glad to be of service :)