+ Start a Discussion
Brian Chipman 4Brian Chipman 4 

Apex Test Class PageReference

Beginner with Apex and test classes. Only getting 60% coverage. Any suggestions? Here is the class:

public class SearchAccountCommunity {
    public string searchEmail {get; set;}
   
    public PageReference findAccount(){
        List<Account> accountId = [SELECT Id from Account WHERE PersonEmail =:searchEmail LIMIT 1];
        PageReference registerNew;
        if(!accountId.isEmpty()){
            string url = 'https://myOrg.force.com/Residential/AssetOverrideCommunity?Account_lkid='+ accountId[0].id;
            registerNew = new PageReference(url);
            registerNew.setRedirect(true);
            return registerNew;
        }
        return registerNew;
    }

}

Here is the test class:

@isTest
public class test_SearchAccountCommunity {
    static testmethod void vailidateFindAccount(){
        SearchAccountCommunity test = new SearchAccountCommunity();
        test.searchEmail = 'test.email@email.com';
        string resultUrl = 'https://myOrg.force.com/Residential/AssetOverrideCommunity?Account_lkid=001j0000014FpHVAA0&isdtp=p1';
        PageReference pageTest = test.findAccount();
        System.assertEquals(resultUrl, pageTest.getUrl());
    }
}
 
Best Answer chosen by Brian Chipman 4
Brian Chipman 4Brian Chipman 4
I ended up hard-coding the RecordTypeId and was able to get 91% coverage with this test class:
 
@isTest
public class SearchAccountCommunity_test {

    static testmethod void vailidateFindAccount(){
        
		//String RecTypeId= [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id;

		Account Accnt = new Account(
            //RecordTypeID=RecTypeId,
			RecordTypeID='012j0000000zJHe',
			FirstName='Test FName',
			LastName='Test LName',
			PersonMailingStreet='Easy Street',
			PersonMailingPostalCode='12345',
			PersonMailingCity='SanFran',
			PersonEmail ='test.email@email.com',
			PersonHomePhone='1234567890'
		);
		insert Accnt;

        SearchAccountCommunity test = new SearchAccountCommunity();
        test.searchEmail = 'test.email@email.com';
        test.url = 'https://myOrg.force.com/Residential/AssetOverrideCommunity?Account_lkid='+ Accnt.id;
        test.findAccount();
		
    }
    
}

Thought I could cover the "string url" line in the original class with the "test.url" line in the test class but it's still the only uncovered line of code. Good enough I suppose.  Thanks both of you for your help.  I learned a great deal from your responses.

All Answers

v varaprasadv varaprasad
Hi Brian,

Please check once following sample code : 
 
@isTest
private class SearchAccountCommunity_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Account account_Obj = new Account(Name = 'Name541', PersonEmail  = 'Email67@test.com');
    Insert account_Obj; 
    test.stopTest();
  }
  static testMethod void test_findAccount_UseCase1(){
    List<Account> account_Obj  =  [SELECT Id,Name,PersonEmail from Account];
    System.assertEquals(true,account_Obj.size()>0);
    SearchAccountCommunity obj01 = new SearchAccountCommunity();
    obj01.searchEmail = 'Email67@test.com';
    obj01.findAccount();
  }
}

Hope this helps you!
If the above information is informative and helps you.Please mark it as the best answer.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
Brian Chipman 4Brian Chipman 4
Hi Varaprasad.  Appreciate the response. Seems very promising. However, in the setupTestData method, the Insert fails because 'Business Account may not use Person Account field personEmail'.  How do I ensure a PersonAccount is being created (these are indeed PersonAccounts we are searching for in the code)?  Have tried a variety of things including this modification:

RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
Account account_Obj = new Account(RecordType = personAccountRecordType, Name = 'Name541', PersonEmail  = 'Email67@test.com');

 
Amit Chaudhary 8Amit Chaudhary 8
Note: If your organization has enabled person accounts, you have two different kinds of accounts: business accounts
and person accounts. If your script creates a new account using name, a business account is created. If the script uses
LastName, a person account is created.

Try to update your test class like below
@isTest
public class test_SearchAccountCommunity 
{
    static testmethod void vailidateFindAccount()
	{
		String RecTypeId= [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id;

		Account Accnt = new Account(
			RecordTypeID=RecTypeId,
			FirstName='Test FName',
			LastName='Test LName',
			PersonMailingStreet='test@yahoo.com',
			PersonMailingPostalCode='12345',
			PersonMailingCity='SFO',
			PersonEmail='test.email@email.com',
			PersonHomePhone='1234567',
			PersonMobilePhone='12345678'
		);
		insert newAccount;

	
        SearchAccountCommunity test = new SearchAccountCommunity();
        test.searchEmail = 'test.email@email.com';
        test.findAccount();
		
    }
}

Let us know if this will help you
 
Brian Chipman 4Brian Chipman 4
Hi Amit - thanks for the reply. We do indeed have person accounts activated.  Your code looks good too. Line 19 change "insert newAccount;" to "insert Acct;"

Your query returns no results.  The log shows "System.QueryException: List has no rows for assignment to SObject". So we are still looking to create a person account in the test class.  Am continuing to research and work on it.  I think we are close because of yours and Varaprasad's help.
Brian Chipman 4Brian Chipman 4
I ended up hard-coding the RecordTypeId and was able to get 91% coverage with this test class:
 
@isTest
public class SearchAccountCommunity_test {

    static testmethod void vailidateFindAccount(){
        
		//String RecTypeId= [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id;

		Account Accnt = new Account(
            //RecordTypeID=RecTypeId,
			RecordTypeID='012j0000000zJHe',
			FirstName='Test FName',
			LastName='Test LName',
			PersonMailingStreet='Easy Street',
			PersonMailingPostalCode='12345',
			PersonMailingCity='SanFran',
			PersonEmail ='test.email@email.com',
			PersonHomePhone='1234567890'
		);
		insert Accnt;

        SearchAccountCommunity test = new SearchAccountCommunity();
        test.searchEmail = 'test.email@email.com';
        test.url = 'https://myOrg.force.com/Residential/AssetOverrideCommunity?Account_lkid='+ Accnt.id;
        test.findAccount();
		
    }
    
}

Thought I could cover the "string url" line in the original class with the "test.url" line in the test class but it's still the only uncovered line of code. Good enough I suppose.  Thanks both of you for your help.  I learned a great deal from your responses.
This was selected as the best answer