Skip to main content
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
3 respuestas
  1. 23 dic 2015, 17:35
    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.
0/9000