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
Kakasaheb EKKakasaheb EK 

Test Class Coverage is not increasing

Hi Guys,

I am trying achieve 100 % test class coverage , but somehow I am not able to achieve.

Could you please help me here..!!

Trigger Code : 

trigger DuplicateStudent on Student__c (before insert) {
       //Get all Student__c related to the incoming Student records in a single SOQL query.
       Student__c[] studentsList = Trigger.new;
       Set<String> emailSet = new Set<String>();
       for(Student__c s : studentsList){
        emailSet.add(s.Email__c);
       }
       //Get list of duplicate Students
       List<Student__c> duplicateStudentList = [Select s.Name, s.Email__c From Student__c s where s.Email__c IN :emailSet];
       Set<ID> duplicateEmailIds = new Set<ID>();
       for(Student__c s : duplicateStudentList){
        duplicateEmailIds.add(s.Email__c);
       }
       for(Student__c s : studentsList){
            if(duplicateEmailIds.contains(s.Email__c))              {
                s.Email__c.addError('Record already exist with same email Id');
            }
       }
}

Test ClasS : 

@isTest
private class DuplicateStudentTrigger_Test {
    
    
         static testMethod void myUnitTest() {
        Account acc = new Account();
        acc.Name = 'SFDC';       
        insert acc;

        Student__c s = new Student__c();
        s.Name = 'Om Test';
        s.Email__c = 'admin@JitendraZaa.com';
        s.Account_Name__c=acc.ID;
        
                   
        try
        {
            insert s;
        }
        catch(System.DMLException e)
        {
            System.assert(e.getMessage().contains('Record already exist with same email Id'));
        }
    }
}
Rajneesh Ranjan 23Rajneesh Ranjan 23
Hi Sachin, 

Seems there is an error in your trigger where you have defined a 'set' to hold email address of students, but the type you mentined is 'ID'
Set<ID> duplicateEmailIds = new Set<ID>();
for(Student__c s : duplicateStudentList){
    duplicateEmailIds.add(s.Email__c);
}
Once you correct this and define a set of type 'String', you would be able to run the logic correctly.
Set<String> duplicateEmailIds = new Set<String>();
for(Student__c s : duplicateStudentList){       
    duplicateEmailIds.add(s.Email__c);
}
Further, you can have a look to the below test class which will help you to get 100% code coverage.
@isTest
private class DuplicateStudentTrigger_Test {

    static testMethod void myUnitTest() {
        
        Account acc = new Account();
        acc.Name = 'SFDC';
        insert acc;
        
        //Create first student record
        Student__c std1 = createStudent('Om Test', 'admin@JitendraZaa.com', acc.Id);
        System.assertEquals('Om Test',std1.Name);

        //Create dupicate student record
        try {
            Student__c std2 = createStudent('Om Test1', 'admin@JitendraZaa.com', acc.Id);
        } catch (System.DMLException e) {
            System.assert(e.getMessage().contains('Record already exist with same email Id'));
        }

    }
    
    static Student__c createStudent(String sName, String sEmail, Id accId){
        Student__c std = new Student__c();
        std.Name = sName;
        std.Email__c = sEmail;
        std.Account_Name__c = accId;
        insert std;
        return std;
    }
}
Hope this will help :)

Thanks,
Rajneesh