+ Start a Discussion
Renuka SharmaRenuka Sharma 

how to write test class for apex code

hey Guys

i need a small help regarding a test classes. Basically i am trying to design a test class for account and contact, in my case Account is Renamed as (Courses) and Contact is Renamed as (Students), and i have 2 custom objects Classes(Class__c) and Attendancess (Attendancess__c)

how do i design a test class for below apex class
public class UpdateUsingCheckboxC {
    
    @AuraEnabled
    public static List <Contact> fetchContact(string key) {
        system.debug('classname'+key);
        Class__c cls = [select id,Courses__r.name from Class__c where id=:key];
        string classname = cls.Courses__r.name;
        system.debug('classname'+classname);
        return [SELECT Id, Name,Account.Name,FirstName,LastName,Check_for_Attendance__c,Check_for_Absentee__c FROM Contact where (Check_for_Attendance__c=false AND Check_for_Absentee__c=false) AND Account.Name =:classname];
    }
    
    @AuraEnabled
    public static void updateRecord(List <String> lstRecordId,string key) {
        List<Contact> lstUpdate = new List<Contact>();
        List<Attendancess__c> listAttendnce = new List<Attendancess__c>();
        Class__c cls = [select id,Courses__r.name from Class__c where id=:key];
        if(cls.Id !=null){
            for(Contact con : [SELECT Id,AccountId, Name,Phone,FirstName,LastName,Check_for_Attendance__c,Check_for_Absentee__c  FROM Contact WHERE Id IN : lstRecordId]){
                con.Check_for_Attendance__c = true;
                con.Check_for_Absentee__c = false;
                lstUpdate.add(con);
                
                Attendancess__c attandenc = new Attendancess__c();
                attandenc.Class__c = cls.Id;
                attandenc.Student_Attended__c = true;
                attandenc.Name = con.Name;
                attandenc.Created_Date_Time__c = system.now();
                listAttendnce.add(attandenc);
                system.debug('listAttendnce'+listAttendnce);
            }
            
            if(lstUpdate.size() > 0){
                update lstUpdate;
            }
            if(listAttendnce.size() > 0){
                insert listAttendnce;
            }
        }
    }
    
    @AuraEnabled
    public static void updateSecondRecord(List <String> secondRecordId,string key) {
        system.debug('key1'+key);
        system.debug('secondRecordId '+secondRecordId);
        List<Contact> lstUpdate = new List<Contact>();
        List<Attendancess__c> listAttendnce = new List<Attendancess__c>();
        if(key !=null){
        Class__c cls = [select id,Courses__r.name from Class__c where id=:key];
            for(Contact con : [SELECT Id,AccountId, Name,Phone,FirstName,LastName,Check_for_Attendance__c,Check_for_Absentee__c  FROM Contact WHERE Id IN : secondRecordId]){
                con.Check_for_Absentee__c = true;
                con.Check_for_Attendance__c = false;
                lstUpdate.add(con);
                
                Attendancess__c attandenc = new Attendancess__c();
                attandenc.Class__c = cls.Id;
                attandenc.Student_Attended__c = false;
                attandenc.Name = con.Name;
                attandenc.Created_Date_Time__c = system.now();
                listAttendnce.add(attandenc);
                system.debug('listAttendnce'+listAttendnce);
            }
            
            if(lstUpdate.size() > 0){
                update lstUpdate;
            }
            if(listAttendnce.size() > 0){
                insert listAttendnce;
            }
        }
    }
    
}

 
Best Answer chosen by Renuka Sharma
FARSANA PSFARSANA PS
Hai Manjunath,

Try this test class...
@isTest
public class UpdateUsingCheckBoxCTest {
    @testSetup static void testData() {
        Account new_cource=new Account(name='bba');
        insert new_course;
        class__c new_class=new class__c(course__c=new_course.id);
        insert new_class;
        contact new_contact=new contact(FirstName='test',LastName='contact',Check_for_Attendance__c=false,Check_for_Absentee__c=false,AccountId=new_course.id);
         insert new_contact;
}
 @isTest static void testfetchContact() {
     String classid=[select id from class__c limit 1].id;
     list<contact> contact_list=UpdateUsingCheckboxC.fetchContact(classid); 
     system.assertNotEquals(null, contact_list)
    }
    
    @isTest static void testupdateRecord() {
       String classid=[select id from class__c limit 1].id;
       string conId=[select id from contact limit 1].id;
        list<string> idList=new list<string>();
        idList.add(conId);
        UpdateUsingCheckboxC.updateRecord(idList,classid);
        list<Attendancess__c> new_attendance=[select id,name,class__c from Attendancess__c];
        System.assertEquals('true', new_attendance[0].Student_Attended__c );
    }
    
    @isTest static void testupdateSecondRecord() {
        String classid=[select id from class__c limit 1].id;
       string conId=[select id from contact limit 1].id;
        list<string> idList=new list<string>();
        idList.add(conId);
        UpdateUsingCheckboxC.updateSecondRecord(idList,classid);
        list<Attendancess__c> new_attendance=[select id,name,class__c from Attendancess__c];
        System.assertEquals('false', new_attendance[0].Student_Attended__c );
    }
}

Hope this will help...
 

All Answers

FARSANA PSFARSANA PS
Hai Manjunath,

Try this test class...
@isTest
public class UpdateUsingCheckBoxCTest {
    @testSetup static void testData() {
        Account new_cource=new Account(name='bba');
        insert new_course;
        class__c new_class=new class__c(course__c=new_course.id);
        insert new_class;
        contact new_contact=new contact(FirstName='test',LastName='contact',Check_for_Attendance__c=false,Check_for_Absentee__c=false,AccountId=new_course.id);
         insert new_contact;
}
 @isTest static void testfetchContact() {
     String classid=[select id from class__c limit 1].id;
     list<contact> contact_list=UpdateUsingCheckboxC.fetchContact(classid); 
     system.assertNotEquals(null, contact_list)
    }
    
    @isTest static void testupdateRecord() {
       String classid=[select id from class__c limit 1].id;
       string conId=[select id from contact limit 1].id;
        list<string> idList=new list<string>();
        idList.add(conId);
        UpdateUsingCheckboxC.updateRecord(idList,classid);
        list<Attendancess__c> new_attendance=[select id,name,class__c from Attendancess__c];
        System.assertEquals('true', new_attendance[0].Student_Attended__c );
    }
    
    @isTest static void testupdateSecondRecord() {
        String classid=[select id from class__c limit 1].id;
       string conId=[select id from contact limit 1].id;
        list<string> idList=new list<string>();
        idList.add(conId);
        UpdateUsingCheckboxC.updateSecondRecord(idList,classid);
        list<Attendancess__c> new_attendance=[select id,name,class__c from Attendancess__c];
        System.assertEquals('false', new_attendance[0].Student_Attended__c );
    }
}

Hope this will help...
 
This was selected as the best answer
Renuka SharmaRenuka Sharma
Hi Farsana

i tried your code and made few changes and it worked perfectly
 
@isTest
Private class Testupdatecheckbox {
    @testSetup 
    static void testData() {
        Account new_course=new Account(name='Amritya sen');
        insert new_course;
        class__c new_class=new class__c(Name=new_course.id);
        insert new_class;
        contact new_contact=new contact(FirstName='test',LastName='contact',Check_for_Attendance__c=false,Check_for_Absentee__c=false,AccountId=new_course.id);
         insert new_contact;
}
 @isTest
    static void testfetchContact() {
     String classid=[select id from class__c limit 1].id;
     list<contact> contact_list=UpdateUsingCheckboxC.fetchContact(classid); 
     system.assertNotEquals(null, contact_list);
    }
    
    @isTest
    static void testupdateRecord() {
       String classid=[select id from class__c limit 1].id;
       string conId=[select id from contact limit 1].id;
        list<string> idList=new list<string>();
        idList.add(conId);
        UpdateUsingCheckboxC.updateRecord(idList,classid);
        list<Attendancess__c> new_attendance=[select id,name,class__c,Student_Attended__c from Attendancess__c];
        System.assertEquals(true, new_attendance[0].Student_Attended__c );
    }
    
    @isTest static void testupdateSecondRecord() {
        String classid=[select id from class__c limit 1].id;
       string conId=[select id from contact limit 1].id;
        list<string> idList=new list<string>();
        idList.add(conId);
        UpdateUsingCheckboxC.updateSecondRecord(idList,classid);
        list<Attendancess__c> new_attendance=[select id,name,class__c,Student_Attended__c from Attendancess__c];
        System.assertEquals(false, new_attendance[0].Student_Attended__c );
    }
}