function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John Neilan 18John 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
@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;  
    }   
}

 
Best Answer chosen by John Neilan 18
Andrew GAndrew G
I would try 3 things.

1.  Move your Test.teststart();  to just after the Acct_Address record insertion

2.  adjust the setting of the variable in the testpage
testPage.getParameters().put('DelAddrId', String.valueOf(testAcctAddr.Id));
3. switch the variable set and the test page set commands
Test.setCurrentPage(testPage);
testPage.getParameters().put('DelAddrId', String.valueOf(testAcctAddr.Id));

regards
Andrew

All Answers

Andrew GAndrew G
I would try 3 things.

1.  Move your Test.teststart();  to just after the Acct_Address record insertion

2.  adjust the setting of the variable in the testpage
testPage.getParameters().put('DelAddrId', String.valueOf(testAcctAddr.Id));
3. switch the variable set and the test page set commands
Test.setCurrentPage(testPage);
testPage.getParameters().put('DelAddrId', String.valueOf(testAcctAddr.Id));

regards
Andrew
This was selected as the best answer
John Neilan 18John Neilan 18
Thanks Andrew.  Those changes didn't work for me, but they did lead me to add a line to my controller in my deleteAddress method: 
Id addressId = apexPages.currentPage().getParameters().get('Id');
Adding this gave me 100% coverage on my test.  Thanks!