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
Bryan JimenezBryan Jimenez 

Simple SOQL Query and Test Help

Hi,
I have created a simple Soql Query to Query a list of Properties 
public class PropertiesUptown {
Public List<McLabs2__Property__c> getPropertiesUptown() {
       List<McLabs2__Property__c> PropertiesUptown=[Select Name,Primary_Contact_Name__c,McLabs2__Loan_Balance__c,Loan_Origination_Date_v2__c,McLabs2__Loan_Maturity_Date__c
                                                                 From McLabs2__Property__c
                                                                 Where CustomField = Test
                                                                 And CustomField = Test2];
                                                                                          
        return PropertiesUptown;
}
}

That Query Works just fine when I use it. However, When trying to test it for deployment, I cant seem to get my test to work. 

This is my test class.
@isTest
public class PropertiesUptownTest 
{ 
    public static testMethod void TestSOQL() 
    {
        McLabs2__Property__c prop = new McLabs2__Property__c();
        // Add all required field
        prop.Activity_Notes__c='test';
        insert prop;


        PropertiesUptown testCont = new PropertiesUptown ();
        List<McLabs2__Property__c> lstProperty = testCont.getPropertiesUptown();
        
        System.assert( lstProperty != null);
        
    }
}

When I run this test, Code Coverage stays at none. Why would this be happening?

Any help is appreciated.

Thank you
Best Answer chosen by Bryan Jimenez
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- Please add all required field while creating the test data.

Please try below code
@isTest
public class PropertiesUptownTest 
{ 
    public static testMethod void TestSOQL() 
    {
        McLabs2__Property__c prop = new McLabs2__Property__c();
        // Add all required field--- You need to add all required field data here
		prop.McLabs2__Property_Address__c ='test';
        prop.LG_Mtg_Exp_Alert__c=True;
        prop.Bucket_District__c='Uptown';
        insert prop;


        PropertiesUptown testCont = new PropertiesUptown ();
        List<McLabs2__Property__c> lstProperty = testCont.getPropertiesUptown();
        
        System.assert( lstProperty != null);
        
    }
}
Let us know if this will help you

Thanks
Amit Chaudhary
 

All Answers

Nagendra ChinchinadaNagendra Chinchinada
Hi Bryan,

Did you insert Customfield values queried as Test,Test2  in test class ?
 
@isTest
public class PropertiesUptownTest 
{ 
    public static testMethod void TestSOQL() 
    {
        McLabs2__Property__c prop = new McLabs2__Property__c();
// Replace below lines with custom field and value  as you give in Where condtioin in query.
        prop.custonfield = Test ;
        prop.customfield = Test2;


        prop.Activity_Notes__c='test';
        insert prop;


        PropertiesUptown testCont = new PropertiesUptown ();
        List<McLabs2__Property__c> lstProperty = testCont.getPropertiesUptown();
        
        System.assert( lstProperty != null);
        
    }
}


 
Bryan JimenezBryan Jimenez
Hi Nagendra, 

i did did not use the values as test and test2, I was just giving an example of what I had done. I actually wrote a value in for the test. Should I have used test and test2 as my values?
Nagendra ChinchinadaNagendra Chinchinada
What I mean was , u have to give same values in that fields for the record ur inserting in test class. R u giving same values for same fields as given in where condition?

If it doesn't work, then better u paste ur exact code here instead of giving examples, so we can find what's exact problem.
Santosh Kumar 275Santosh Kumar 275
The code you posted over here is working fine. After running the test class the code coverage is 100% so kindly paste the orginal code where you are facing the problem.
Bryan JimenezBryan Jimenez
Hi, This is my original code.

This is my Query.
public class PropertiesUptown {
Public List<McLabs2__Property__c> getPropertiesUptown() {
       List<McLabs2__Property__c> PropertiesUptown=[Select Name,Primary_Contact_Name__c,McLabs2__Loan_Balance__c,Loan_Origination_Date_v2__c,McLabs2__Loan_Maturity_Date__c
                                                                 From McLabs2__Property__c
                                                                 Where LG_Mtg_Exp_Alert__c = True
                                                                 And Bucket_District__c = 'Uptown'];
                                                                                          
        return PropertiesUptown;
}
}

and this is my test class.
@isTest
public class PropertiesUptownTest 
{ 
    public static testMethod void TestSOQL() 
    {
        McLabs2__Property__c prop = new McLabs2__Property__c();
        // Add all required field
        prop.LG_Mtg_Exp_Alert__c=True;
        prop.Bucket_District__c='Uptown';
        insert prop;


        PropertiesUptown testCont = new PropertiesUptown ();
        List<McLabs2__Property__c> lstProperty = testCont.getPropertiesUptown();
        
        System.assert( lstProperty != null);
        
    }
}



Thank you for all your help
Amit Chaudhary 8Amit Chaudhary 8
Hi Bryan Jimenez,

Your code look good to me. What error you are getting ?
Please execute test class from Salesforce UI.
 
Bryan JimenezBryan Jimenez
Hi Amit,

The Error that I am getting is 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [McLabs2__Property_Address__c]: [McLabs2__Property_Address__c]
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- Please add all required field while creating the test data.

Please try below code
@isTest
public class PropertiesUptownTest 
{ 
    public static testMethod void TestSOQL() 
    {
        McLabs2__Property__c prop = new McLabs2__Property__c();
        // Add all required field--- You need to add all required field data here
		prop.McLabs2__Property_Address__c ='test';
        prop.LG_Mtg_Exp_Alert__c=True;
        prop.Bucket_District__c='Uptown';
        insert prop;


        PropertiesUptown testCont = new PropertiesUptown ();
        List<McLabs2__Property__c> lstProperty = testCont.getPropertiesUptown();
        
        System.assert( lstProperty != null);
        
    }
}
Let us know if this will help you

Thanks
Amit Chaudhary
 
This was selected as the best answer
Bryan JimenezBryan Jimenez
This worked! Thank you. I wasnt aware that I had to add all required fields. Thank you again.