0

Having some issues with my test class code coverage. It's at 90% coverage but will not allow me to deploy to production because of this error:

"System.QueryException: List has no rows for assignment to SObject"

Stack Trace:

"Class.deleteRowsDevNotes.onloadmethod: line 16, column 1 Class.deleteRowsDevNotesTest3.start: line 50, column 1"

Can anybody assist by pointing out what needs to be added to my "start" method?

Class:

    public class deleteRowsDevNotes {
//Variable to hold all accounts added/edited
public List<Sales_Dev_Notes__c> allContactList = new List<Sales_Dev_Notes__c>();

//Variable to hold add contacts to be deleted
public List<Sales_Dev_Notes__c> deleteContactList = new List<Sales_Dev_Notes__c>();

//Variable to hold the Contact record
public User accountRec {get;set;}

//Called when the page loads initially from the "action" method on the apex:page. Populates the Account record and the releated contact list
    public void onloadmethod() {

     List<Sales_Dev_Notes__c> allContactList1 = new List<Sales_Dev_Notes__c>();
     String aid =System.currentPageReference().getParameters().get('id');
     accountRec = [SELECT Id,Name,Profile.Name,Title,ManagerId,Extension,Current_Month_Quota__c,UserRole.Name FROM User WHERE Id=:aid]; 
     allContactList = [SELECT Id, OwnerId, CreatedById, CreatedDate, Notes__c, Coach_Type__c, Creator__c, Note_Date__c, Action__c FROM Sales_Dev_Notes__c WHERE OwnerId=:accountRec.Id ORDER BY Note_Date__c DESC];
}
//Send the list of contacts to the visualforce page
public List<Sales_Dev_Notes__c> getContacts(){
    return allContactList;
}

//Add a temporary contact to the table. Not saved to the database
public void addContact(){
    Sales_Dev_Notes__c c = new Sales_Dev_Notes__c();
    allContactList.add(c);
}

//Remove a contact from the table.
public void removeContact(){
    Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index'));
    //If the contact is an existing contact then add it to the list to delete from the databse
    if(allContactList[indexVal - 1].Id != null)
        deleteContactList.add(allContactList[indexVal - 1]);
    //Remove the contact from the table    
    allContactList.remove(indexVal - 1);            
}    
public void saveChanges(){
    //update existing contacts and insert new ones
    upsert allContactList;
    //delete the contacts that were removed
    if(deleteContactList.size() > 0)
        delete deleteContactList;
}    

}

AND Test Class:

    @isTest
private class deleteRowsDevNotesTest3 {

    static testMethod void myUnitTest() {
        deleteRowsDevNotes  clsObj = new deleteRowsDevNotes();
        /*Sales_Dev_Notes__c acct =  new Sales_Dev_Notes__c();
        acct.Name = 'testAccount';*/

        Apexpages.currentpage().getParameters().put('index','1');

       clsObj.addContact();


        clsObj.saveChanges();  
        clsObj.removeContact();  
    }

     static testmethod void start()
   {

   User accountRec1 = New User();
   accountRec1.LastName =  'Test';
   accountRec1.Alias = 'aTest';
   accountRec1.Email = '[email protected]';
   accountRec1.Username = '[email protected]';
   accountRec1.CommunityNickname = 'aTest';
   accountRec1.EmailEncodingKey = 'UTF-8';
   accountRec1.LanguageLocaleKey = 'en_US';
   accountRec1.LocaleSidKey = 'en_US';
   accountRec1.TimeZoneSidKey='America/Los_Angeles';
   accountRec1.ProfileId = '00e50000001Be3P';
   accountRec1.IsActive = true;
   Insert accountRec1 ; 

   system.runas(accountRec1) {
   date runtime=system.today();
   Sales_Dev_Notes__c allContactList1 = New Sales_Dev_Notes__c();
   allContactList1.Notes__c =  'Test';
   allContactList1.Coach_Type__c = 'aTest';
   allContactList1.Creator__c = 'Paul Gentile';
   allContactList1.Action__c = '[email protected]';
   allContactList1.OwnerId = '00550000001R0Fl';
   allContactList1.Note_Date__c = runtime;
   Insert allContactList1 ; 

   deleteRowsDevNotes qdnc = new deleteRowsDevNotes();
   test.starttest();
   qdnc.accountRec = accountRec1;

   qdnc.onloadmethod();


   qdnc.clone();

   qdnc.addContact();

   qdnc.saveChanges();

test.stoptest();

}
   }


   static testMethod void testdeleteRowsDevNotes() {
deleteRowsDevNotes clsObj = new deleteRowsDevNotes();
    Test.startTest();
    Sales_Dev_Notes__c [] notes = clsObj.getContacts();
    Test.stopTest();
    System.assertNotEquals(null, notes);
    }

}
1
  • 1
    Which line is 50, and which one is 16? Jan 3, 2017 at 22:31

1 Answer 1

0

According to my understanding, you need to assign userId which you are trying to retrieve in onloadmethod() method and using that in SOQL.

Also, you have to create the Sales_Dev_Notes__c record with the ownerId as accountRec1.Id.

finally, remove hard coded id in the test class.

And follow best practices (as no assert statements are found in your method.)

Refer How to Write Good Unit Tests and Testing Best Practices

The updated code is:

static testmethod void start()
   {

       User accountRec1 = New User();
       accountRec1.LastName =  'Test';
       accountRec1.Alias = 'aTest';
       accountRec1.Email = '[email protected]';
       accountRec1.Username = '[email protected]';
       accountRec1.CommunityNickname = 'aTest';
       accountRec1.EmailEncodingKey = 'UTF-8';
       accountRec1.LanguageLocaleKey = 'en_US';
       accountRec1.LocaleSidKey = 'en_US';
       accountRec1.TimeZoneSidKey='America/Los_Angeles';
       accountRec1.ProfileId = '00e50000001Be3P';
       accountRec1.IsActive = true;
       Insert accountRec1 ; 

   system.runas(accountRec1) {
       date runtime=system.today();
       Sales_Dev_Notes__c allContactList1 = New Sales_Dev_Notes__c();
       allContactList1.Notes__c =  'Test';
       allContactList1.Coach_Type__c = 'aTest';
       allContactList1.Creator__c = 'Paul Gentile';
       allContactList1.Action__c = '[email protected]';
       allContactList1.OwnerId = accountRec1.id;  //'00550000001R0Fl';
       allContactList1.Note_Date__c = runtime;
       Insert allContactList1 ; 

       deleteRowsDevNotes qdnc = new deleteRowsDevNotes();
       test.starttest();

       qdnc.accountRec = accountRec1;
       Apexpages.currentpage().getParameters().put('id',accountRec1.Id);

       qdnc.onloadmethod();


       qdnc.clone();

       qdnc.addContact();

       qdnc.saveChanges();

        test.stoptest();

    }
 }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .