+ Start a Discussion
kullayappa Adapalakullayappa Adapala 

how to cover the test class for Loop?

How to Cover the test class for Loop?
 

User-added image
@istest

Private class dummy_email_testclass{
     static testMethod void dummy_email_testclassa(){
         Contact con=new Contact();
         
         COn.Lastname='test';
         con.Email='test@gmail.com';
         
         insert con;
         update con;
         
     }
}



Best Answer chosen by kullayappa Adapala
Aman MalikAman Malik
Hi kullayappa,
Insert both the record as your trigger fires on insert not on update, don't insert one and update other.
@istest
Private class dummy_email_testclass {
    static testMethod void dummy_email_testclassa(){
        Contact con=new Contact();
        Con.Firstname = 'firstname';
        Con.Lastname='test';
        con.Email='test@gmail.com';
        
        insert con;
        
        Contact con1=new Contact();
        con1.id = con.id;
        Con1.Firstname = 'firstname1';
        Con1.Lastname='test1';
        con1.Email='test1@gmail.com';
        
        insert con1;
        
    }
}

Thanks,
Aman
 

All Answers

Aman MalikAman Malik
Hi kullayappa,

In the above code, the reason why for loop is not covering is because your map 'varmap' is empty. and this is empty because you don't have enough(more than one record) data.
So, you must have to create atleast 2 contact test record in your test class to cover your test code. Kindly give it one more try with 2 record.

Please mark the answer as best if this helps.

Thanks,
Aman
Suraj TripathiSuraj Tripathi
Hi  Kullayappa Adapala,

Please Try the following test class it covers your class 88% because you are using adderror method that fails in test class so iam adding test.isrunningtest() in your apex trigger.

Apex Trigger
trigger dummy_email on Contact (before insert) {
	List<Contact> con =[Select id,name,Email from Contact];
    
    map<String,id> varmap=new map<String,Id>();
    
    for(contact conss:con){
        varmap.put(conss.Email,conss.id);
    }
    if(!varmap.isempty()){
        for(Contact connew:trigger.new){
            if(varmap.containsKey(connew.Email)){
                if(!test.isRunningTest()){ //for skip add error method in test class
                     connew.adderror('ssssssss');
                }
            }
        }
    }
}

Apex test class
@istest
Private class dummy_email_testclass {
    static testMethod void dummy_email_testclassa(){
        Contact con=new Contact();
        Con.Firstname = 'firstname';
        Con.Lastname='test';
        con.Email='test@gmail.com';
        
        insert con;
        
        Contact con1=new Contact();
        Con1.Firstname = 'firstname1';
        Con1.Lastname='test1';
        con1.Email='test@gmail.com';
        
        insert con1;
        
    }
}

Mark as a best if helpfull to you

Regards,
Suraj
kullayappa Adapalakullayappa Adapala
Hi Suraj Tripathi,

I have used which you have provided the code , but it is not working and for loop is not covered in the test class.
 
@istest
Private class dummy_email_testclass {
    static testMethod void dummy_email_testclassa(){
        Contact con=new Contact();
        Con.Firstname = 'firstname';
        Con.Lastname='test';
        con.Email='test@gmail.com';
        
        insert con;
        
        Contact con1=new Contact();
        con1.id = con.id;
        Con1.Firstname = 'firstname1';
        Con1.Lastname='test1';
        con1.Email='test1@gmail.com';
        
        update con1;
        
    }
}


Thanks

kullayappa
 
Aman MalikAman Malik
Hi kullayappa,
Insert both the record as your trigger fires on insert not on update, don't insert one and update other.
@istest
Private class dummy_email_testclass {
    static testMethod void dummy_email_testclassa(){
        Contact con=new Contact();
        Con.Firstname = 'firstname';
        Con.Lastname='test';
        con.Email='test@gmail.com';
        
        insert con;
        
        Contact con1=new Contact();
        con1.id = con.id;
        Con1.Firstname = 'firstname1';
        Con1.Lastname='test1';
        con1.Email='test1@gmail.com';
        
        insert con1;
        
    }
}

Thanks,
Aman
 
This was selected as the best answer
kullayappa Adapalakullayappa Adapala
Thanks, Aman Malik Issue Solved

Thank you so much for your helping
Suraj TripathiSuraj Tripathi

Hi Kullayappa Adapala,

Please check my test class because which test class you provided that is not mine and it is inserting with contactID that can not be possible.

Please make us confirm

Thanks