• Jason Liu 7
  • NEWBIE
  • 20 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Yo Team,

I'm new to the apex development and really confuse how to use contructor in the Class. So I look other Sr.developer's code and mimic the apex class, I'm so lucky that works. But turn to Test, I kind stuck there. Following is my Apex class and test class(not working) I created, I truely appriate that if anyone could give me some advises.

Apex Class:

public class HouseholdpageController {
    List<Account> accounts;
    public Lead currentlead{get;set;}
    public ApexPages.StandardSetController stdCntrlr {get; set;}
    public HouseholdpageController (ApexPages.StandardController controller)
  {     
    this.currentlead = (Lead)controller.getRecord(); 
    }
    public List<Account> getAccounts(){
        currentlead = [ Select Id, Equifax_Household_ID__c from Lead where Id =: ApexPages.currentPage().getParameters().get('Id') ];
        if(currentlead.Equifax_Household_ID__c != null) accounts = [select name, Loan_Balance__c, Deposit_Balance__c from account where Equifax_Household_ID__c =: currentlead.Equifax_Household_ID__c];
     
        return accounts;
    }

}


Test Class(Error:System.QueryException: List has no rows for assignment to SObject)
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class HouseholdpageControllerTest {
    static testMethod void UnitTest() {
        RecordType rt = [SELECT Id,Name FROM RecordType WHERE SobjectType='Account' AND Name = 'Person Account'];   
        RecordType lead = [select Id from RecordType where DeveloperName = 'Relationship_Expansion_Leads']; 
        
        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'
        );
        
        Account testAccount1 = new Account(       
          FirstName='Test Description', 
          LastName='Test Last Name',
          RecordTypeId = rt.Id,
        Equifax_Household_ID__c = '123',
        Loan_Balance__c = 321,
        Deposit_Balance__c  = 456
        );
        Lead testLead1 = new Lead(
            LastName = 'TEST Name 1',
            status = 'Open',
            Phone = '5165039576',
            Email = '1@1.com',
            Classification__c = 'Consumer',
            RecordTypeId = lead.Id,
            Equifax_Household_ID__c = '123'
        ); 
           Test.startTest();
            List<Account> results;
        
            System.runAs(u) {
                insert testLead1;
                insert testAccount1;
            HouseholdpageController ctrl = new HouseholdpageController(new ApexPages.StandardController(testLead1));
            results = ctrl.getAccounts();
            }
        Test.stopTest();
    }
}

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