You need to sign in to do that
Don't have an account?
Kakasaheb 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'));
}
}
}
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'));
}
}
}
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' Once you correct this and define a set of type 'String', you would be able to run the logic correctly. Further, you can have a look to the below test class which will help you to get 100% code coverage. Hope this will help :)
Thanks,
Rajneesh