You need to sign in to do that
Don't have an account?
John Neilan 18
Custom Controller Test Error: System.QueryException: List has no rows for assignment to SObject
Hello,
I am trying to create a test class for a relatively simple custom controller extension. However, I am having trouble covering one of my methods in the controller that handled deletion of records.
I need to somehow pass a value for the DelAddrId variable in my controller from my test class, but I'm not sure how to do that. Can anyone assist?
Test Class
Controller
I am trying to create a test class for a relatively simple custom controller extension. However, I am having trouble covering one of my methods in the controller that handled deletion of records.
I need to somehow pass a value for the DelAddrId variable in my controller from my test class, but I'm not sure how to do that. Can anyone assist?
Test Class
@isTest private class Test_AcctAddressesController { static testMethod void testMethod1() { Account testAccount = test_CreateRecords.createAcct(0); insert testAccount; Contact testContact = test_CreateRecords.createCont(testAccount.Id); insert testContact; Test.StartTest(); Account_Addresses__c testAcctAddr = new Account_Addresses__c(); testAcctAddr.Account__c = testAccount.Id; testAcctAddr.Street__c = '1010 Test Ave'; testAcctAddr.City__c = 'New York'; testAcctAddr.State__c = 'NY'; testAcctAddr.Zip_Postal_Code__c = '11212'; testAcctAddr.Country__c = 'USA'; testAcctAddr.Primary__c = TRUE; testAcctAddr.Current__c = TRUE; insert testAcctAddr; PageReference testPage = Page.Acct_Addresses; testPage.getParameters().put('DelAddrId', testAcctAddr.Id); Test.setCurrentPage(testPage); ApexPages.StandardController AcctAdd = new ApexPages.standardController(testAccount); AcctAddresses AcctAdd1 = new AcctAddresses(AcctAdd); System.assertNotEquals(null,AcctAdd1.deleteAddress()); AcctAdd1.save(); AcctAdd1.deleteAddress(); Test.StopTest(); } }
Controller
public class AcctAddresses { public List<Account_Addresses__c> addresses{get;set;} public string DelAddrId { get; set;} public Account accounts {get;set;} public Account acct {get;set;} //Constructor public AcctAddresses(ApexPages.StandardController controller) { acct = (account)controller.getRecord(); accounts = [SELECT Id FROM account WHERE id=: acct.id LIMIT 1]; addresses = [SELECT Id, Name, Primary__c, Current__c, Street__c, City__c, State__c, Zip_Postal_Code__c, Country__c, Account__c FROM Account_Addresses__c WHERE Account__r.Id = :accounts.id ORDER BY Primary__c, State__c]; } //This method is to edit the existing contact record while clicking the Edit link public pageReference Save(){ update addresses; return ApexPages.CurrentPage(); } //This method is to delete the contact record while clicking the Del link public pageReference deleteAddress(){ Account_addresses__c ToBeDeleted = [SELECT Id FROM Account_addresses__c WHERE id = : DelAddrId LIMIT 1]; delete ToBeDeleted; String baseUrl = URL.getSalesforceBaseUrl().toExternalForm(); PageReference redirectPage = new PageReference(baseUrl+'/'+acct.id); return redirectPage; } }
1. Move your Test.teststart(); to just after the Acct_Address record insertion
2. adjust the setting of the variable in the testpage 3. switch the variable set and the test page set commands
regards
Andrew
All Answers
1. Move your Test.teststart(); to just after the Acct_Address record insertion
2. adjust the setting of the variable in the testpage 3. switch the variable set and the test page set commands
regards
Andrew