You need to sign in to do that
Don't have an account?
Brian 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());
}
}
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());
}
}
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
Please check once following sample code :
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
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');
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
Let us know if this will help you
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.
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.