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
Jason Liu 7Jason Liu 7 

Test Class for SOQL

Hi Team,

I'm new to the apex coding, so I'm wondering if anyone could help me with the test class associate with following SOQL queries.

public class HomePageController {
    
    Public static RecordType Rectyp = [select id from recordType where DeveloperName = 'Relationship_Expansion_Leads'];
    
    public List<Lead> getLeads(){
      List<Lead> leads = [select id, name from lead where owner.id =: UserInfo.getUserId() and status = 'Open' and RecordTypeId =: Rectyp.id ];
    return leads;
       }
}

Thanks,
Jason Liu
Best Answer chosen by Jason Liu 7
pconpcon
I've taken the liberty to rewite your test to make it a little better.
 
@isTest
private class HomePageControllerTest {
    static testMethod void myUnitTest() {
        Profile p = [
            select Id
            from Profile
            where Name = 'System Administrator'
        ];

        RecordType rt = [
            select Id
            from RecordType
            where DeveloperName = 'Relationship_Expansion_Leads'
        ];

        User u = new User(
            Alias = 'standt',
            Email = 'standarduser@testorg.com',
            EmailEncodingKey = 'UTF-8',
            LastName = 'Testing',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            ProfileId = p.Id,
            TimeZoneSidKey = 'America/Los_Angeles',
            UserName='1512434@dfe1.COM'
        );

        Lead testLead1 = new Lead(
            LastName = 'TEST Name 1',
            OwnerId = u.Id,
            status = 'Qualified',
            Phone = '5165039576',
            Email = '1@1.com',
            Classification__c = 'Consumer',
            Status = 'Open',
            RecordTypeId = rt.Id,
            Bank_Relationship_Manager__c = u.Id
        );  
        
        Lead testLead2 = new Lead(
            LastName = 'TEST Name 2',
            OwnerId = u.Id,
            status = 'Qualified',
            Phone = '5165039577',
            Email = '2@2.com',
            Classification__c = 'Consumer',
            Status = 'Closed',
            RecordTypeId = rt.Id,
            Bank_Relationship_Manager__c = u.Id
        );  
        
        Lead testLead3 = new Lead(
            LastName = 'TEST Name 3',
            OwnerId = u.Id,
            status = 'Qualified',
            Phone = '5165039578',
            Email = '3@3.com',
            Classification__c = 'Consumer',
            Status = 'Open',
            RecordTypeId = rt.Id,
            Bank_Relationship_Manager__c = u.Id
        );  
        
        List<Lead> leads = new List<Lead>{
            testLead1,
            testLead2,
            testLead3
        };  
        insert leads;

        Test.startTest();

        List<Lead> results;

        System.runAs(u) {
            HomePageController controller = new HomePageController();
            results = controller.getLeads();
        }

        Test.stopTest();

        Set<Id> expectedResults = new Set<Id>{
            testLead1.Id,
            testLead3.Id
        };

        System.assertEquals(
            expectedResults.size(),
            results.size(),
            'Did not the expected number of leads back'
        );

        for (Lead l : results) {
            System.assert(
                expectedResults.contains(l.Id),
                'Got a lead back we did not expect ' + l.LastName
            );
            expectedResults.remove(l.Id);
        }

        System.assert(
            expectedResults.isEmpty(),
            'Did not get back all the expected leads'
        );
    }

NOTE: This code has not been tested and may contain typographical or logical errors
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.

This will not use a hardcoded record type id, and it sets the OwnerId to what the query is expecting.

All Answers

pconpcon
You would want to create a list of leads that meet your criteria and then insert them.  The call your HomePageCongroller.getLeads() method and verify that the data came back as expected.  I would recommend reading over these [1] [2] [3] [4] articles.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
Jason Liu 7Jason Liu 7
Hi pcon, thank you for your quick response and appriciate the articles. I did create a test case for above code, but looks like it's not cover the class at all. So any idea about that?


@isTest
private class HomePageControllerTest {
     static testMethod void myUnitTest() {
        Profile p = [SELECT Id FROM Profile where Name = 'System Administrator'];
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='1512434@dfe1.COM');

         System.runAs(u){
        //Create Data for Customer Objet
         Lead leads = new Lead();
         leads.LastName = 'TEST Name';
         leads.status = 'Qualified';
         leads.Phone = '5165039576';
         Leads.Email = '1@1.com';
         leads.Classification__c = 'Consumer';
         leads.RecordTypeId = '012q0000000CkoQAAS';
         leads.Bank_Relationship_Manager__c = UserInfo.getUserId();
         insert leads;
          HomePageController controller = new HomePageController();
        
         //Now, our trigger will fire on After update event so update the Records
        Test.startTest();//Starts the scope of test
         
        leads.status  = 'Open';
        update leads;
        controller.getLeads() ;
             
        Test.stopTest();//Ends the scope of test
         
         //Now check if it is giving desired results using system.assert Statement.New invoice should be created
        
         }
    }
}
pconpcon
I've taken the liberty to rewite your test to make it a little better.
 
@isTest
private class HomePageControllerTest {
    static testMethod void myUnitTest() {
        Profile p = [
            select Id
            from Profile
            where Name = 'System Administrator'
        ];

        RecordType rt = [
            select Id
            from RecordType
            where DeveloperName = 'Relationship_Expansion_Leads'
        ];

        User u = new User(
            Alias = 'standt',
            Email = 'standarduser@testorg.com',
            EmailEncodingKey = 'UTF-8',
            LastName = 'Testing',
            LanguageLocaleKey = 'en_US',
            LocaleSidKey = 'en_US',
            ProfileId = p.Id,
            TimeZoneSidKey = 'America/Los_Angeles',
            UserName='1512434@dfe1.COM'
        );

        Lead testLead1 = new Lead(
            LastName = 'TEST Name 1',
            OwnerId = u.Id,
            status = 'Qualified',
            Phone = '5165039576',
            Email = '1@1.com',
            Classification__c = 'Consumer',
            Status = 'Open',
            RecordTypeId = rt.Id,
            Bank_Relationship_Manager__c = u.Id
        );  
        
        Lead testLead2 = new Lead(
            LastName = 'TEST Name 2',
            OwnerId = u.Id,
            status = 'Qualified',
            Phone = '5165039577',
            Email = '2@2.com',
            Classification__c = 'Consumer',
            Status = 'Closed',
            RecordTypeId = rt.Id,
            Bank_Relationship_Manager__c = u.Id
        );  
        
        Lead testLead3 = new Lead(
            LastName = 'TEST Name 3',
            OwnerId = u.Id,
            status = 'Qualified',
            Phone = '5165039578',
            Email = '3@3.com',
            Classification__c = 'Consumer',
            Status = 'Open',
            RecordTypeId = rt.Id,
            Bank_Relationship_Manager__c = u.Id
        );  
        
        List<Lead> leads = new List<Lead>{
            testLead1,
            testLead2,
            testLead3
        };  
        insert leads;

        Test.startTest();

        List<Lead> results;

        System.runAs(u) {
            HomePageController controller = new HomePageController();
            results = controller.getLeads();
        }

        Test.stopTest();

        Set<Id> expectedResults = new Set<Id>{
            testLead1.Id,
            testLead3.Id
        };

        System.assertEquals(
            expectedResults.size(),
            results.size(),
            'Did not the expected number of leads back'
        );

        for (Lead l : results) {
            System.assert(
                expectedResults.contains(l.Id),
                'Got a lead back we did not expect ' + l.LastName
            );
            expectedResults.remove(l.Id);
        }

        System.assert(
            expectedResults.isEmpty(),
            'Did not get back all the expected leads'
        );
    }

NOTE: This code has not been tested and may contain typographical or logical errors
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.

This will not use a hardcoded record type id, and it sets the OwnerId to what the query is expecting.
This was selected as the best answer