0

i am new to apex...

My Apex Class..

public class StudentLists {

    public List<Student__c> stuList{get;set;}

    public String name1 {get;set;}
    public String eid {get;set;}

    Public StudentLists(){

        stuList= new List<Student__c>();
        stuList= [select name,Address__c,phone__c,email__c,zipcode__c from Student__c ];
    }

    public void deleteAll(){

        List<Student__c> studentDeleteAll = new List<Student__c>();
        studentDeleteAll = [SELECT name,Address__c,phone__c from Student__c ];
        delete studentDeleteAll;
    }

    public PageReference  new1(){

        PageReference oPageRef = Page.sturecord;
        oPageRef.setRedirect(true);
        return oPageRef;
    }

    public pageReference edit(){

        PageReference oPageRef = Page.stuEdits;
        oPageRef.setRedirect(true);
        return oPageRef;
    }

    public pageReference deleteOneRecord(){

        List<Student__c> studentDelete = new List<Student__c>();
        System.debug('Name: '+name1);
        studentDelete = [SELECT name, Address__c, phone__c FROM Student__c WHERE name=:name1];
        delete studentDelete;
        PageReference pageRef=Page.StudentList;
        pageRef.setRedirect(true);
        return pageRef;
    }
}

here my test class of deleteOneRecord....

@isTest
    static void testDelete(){

        StudentLists studentList = new StudentLists();
        String beforeDeleteName = [SELECT name FROM student__c ORDER BY name DESC LIMIT 1].name;
        studentList.name1='Aman';
        studentList.deleteOneRecord();
        String afterDeleteName = [SELECT name FROM student__c ].name;
        System.assertNotEquals(beforeDeleteName, afterDeleteName);
    }

How to solve this exception in test class:System.QueryException: List has no rows for assignment to SObject...

deleteOneRecord Work Properly.

2 Answers 2

1

You have no student records in your test method. You need to create and insert one student record or your query will return 0 items and fail.

You can make your code more robust by saving the query into a list and checking that you have at least one record.

2

You get this error when you try to assign the result of a query (which returns no records) to a single instance of a specific sObject, like this...

// Assuming here the query didn't return any rows.
CustomObject customObjectInstance = [SELECT Id, Name FROM CustomObject__c WHERE Name = NULL];

Instead, preferably perform the same as follows...

// Assuming here the query didn't return any rows.
List<CustomObject> lstCustomObjects = [SELECT Id, Name FROM CustomObject__c WHERE Name = NULL];
CustomObject customObjectInstance;
if(lstCustomObjects != NULL && lstCustomObjects.isEmpty() == false) {
    customObjectInstance = lstCustomObjects[0];
}

Regarding the issue

Once you delete the record using your custom method, as it is a test class and you don't have access to the data that exists in the org, the query will return no records. Thus you will not be able to put the asserts as you have now.

You can simply put assert on the size of the list returned by the query (which should be 0).

Hope this helps.

Best Regards!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.