You need to sign in to do that
Don't have an account?
Niknit
Why am i not able to insert test data in my Test class?
About trigger :There are two objects student_details__c and department__c. department__c - contains name and no_of_students__c; student_details__c contains name, department(lookup to department object).
when a student is inserted in a department, that respective department needs to increment the no_of_students__field,similarly when department is changed of a student from one dept to other then no_of_students__c needs to be incremented in the new department and decremented from the old department field. no_of_student__c is the total count of students in respect to the department the belong to in each department object.
Testclass
error is in the insert newstudent line
Trigger
Triggerhandler
when a student is inserted in a department, that respective department needs to increment the no_of_students__field,similarly when department is changed of a student from one dept to other then no_of_students__c needs to be incremented in the new department and decremented from the old department field. no_of_student__c is the total count of students in respect to the department the belong to in each department object.
Testclass
@istest private class StudentupdatehandlerTestClass { static testMethod void validateupdatechange() { Department__c newdept = new department__c(); newdept.name='CSE'; insert newdept; student_details__c newstudent = new Student_details__c(); newstudent.Name='From TestClass'; newstudent.department__c = newdept.id; System.debug('before@@trigger: ' + newdept.no_of_students__c); insert newstudent; newstudent = [SELECT department__c FROM student_details__c WHERE Id =:newstudent.Id]; System.debug('after@@trigger: ' + newdept.no_of_students__c); System.assertEquals(1 , newdept.no_of_students__c); } }
error is in the insert newstudent line
Trigger
trigger Test on Student_Details__c (after insert, after update,after delete) { studentupdatehandler studobj = new studentupdatehandler(); if(trigger.isinsert) { studobj.afterinsert(trigger.new); } if(trigger.isdelete) { studobj.afterdelete(trigger.old); } if(trigger.isupdate) { studobj.afterupdate(trigger.new,trigger.old); } }
Triggerhandler
public class studentupdatehandler{ set<id> deptid = new set<id>(); public void afterinsert(student_details__c[] newstudentslist) { list<department__c> newdeptlist = [select id,name,no_of_students__c,(select id from student_details__r) from department__c where id in:forids(newstudentslist)]; toinsert(newdeptlist); } public void afterdelete(student_details__c[] oldstudentslist) { list<department__c> olddeptlist = [select id,name,no_of_students__c,(select id from student_details__r) from department__c where id in:forids(oldstudentslist)]; toupdate(olddeptlist); } public void afterupdate(student_details__c[] newstudentslist,student_details__c[] oldstudentslist) { list<department__c> newdeptlist = [select id,name,no_of_students__c,(select id from student_details__r) from department__c where id in:forids(newstudentslist)]; toinsert(newdeptlist); list<department__c> odeptlist= [select id,name,no_of_students__c,(select id from student_details__r) from department__c where id in:forids(oldstudentslist)]; toupdate(odeptlist); } Public set<id> forids(student_details__c[] deptlist ){ set<id> ids = new set<id>(); for(student_Details__c record: deptlist){ ids.add(record.Department__c); } return ids;} Public void toinsert(department__c[] deptlist ){ for(department__c dept : deptlist){ dept.no_of_students__c = dept.no_of_students__c +1; } update deptlist; } Public void toupdate(department__c[] odeptlist ){ for(department__c dept : odeptlist){ dept.no_of_students__c = dept.no_of_students__c -1; } update odeptlist; } }
I need to create test case for insert, update and delete.
The problem is I am not bale to input data for the student object for the lookup field in it,Department is another object which is lookup in the student object with field department__c,
I feel i am not populating that field in correct manner in test class.
Here is the code to to test record update.
All Answers
unless there is a specific reason for LOOKUP relation between department__c and student_details__c I would suggest you to change the relation to Master-Detail and use the Roll-Up-Summray fields (https://help.salesforce.com/articleView?id=fields_defining_summary_fields.htm&type=0&language=en_US) to let the platform calculate department__c.no_of_students__c for you
Oops, sorry i thought i had already added the rror message.
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Test: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.studentupdatehandler.toinsert: line 61, column 1 Class.studentupdatehandler.afterinsert: line 12, column 1 Trigger.Test: line 12, column 1: []
The line " newstudent.department__c = newdept.id; " in the test class is what causes the error, the test class passes if i comment that line.
Hi @Asif
I did try that, it seems it is getting inserted. it shows all values of department as inserted.
Hi @Asif
I tried with the below statement in the toinsert()
System.debug('@@@@@deplist ' + deptlist);
it shows the department entered in the Testclass.
"19:40:03:037 USER_DEBUG [54]|DEBUG|@@@@@deplist (Department__c:{Id=a0Q7F000000pH05UAE, Name=CSE, No_of_Students__c=0})"
Did you try manually adding record into Student_details__c with a lookup to DEPT ?
I thought i am manually adding the student_details__c record with lookup tp dept from below lines
student_details__c newstudent = new Student_details__c();
newstudent.Name='From TestClass';
newstudent.department__c = newdept.id;
Hi @Asif ,
The test is running now, the test passes.
the following is the code
How do i test for update, i can delete the record and check for delete but how do i test for update method , what code do i add to test the update method, Can you please help on that update matter please.Thanks you for giving me time and helping me out in this little issue.
Here is the code to to test record update.
Hi Asif, can you please tell me why we are using where Id=:newDept.Id in the SOQL query , when i don't use it test fails, why is it needed?
I was trying to make it on my own, as follows but it fails on update method
Your initial code worked without any filter because you were only inserting single record. After I updated the code with additional Dept insert I need to make sure I am picking the right dept record so the filter on Id. If I dont put the filter I ll get any random record and my Assertions will fail.
valid data - sensible, possible data that the program should accept and be able to process.
extreme data - valid data that falls at the boundary of any possible ranges.
invalid (erroneous) data - data that (https://exams.com.pk/) the program cannot process and should not accept.