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
Joe Rodden 7Joe Rodden 7 

Can't figure out why test class isn't covering 100%

Fairly new so bear with me. I've written a class that throws an error message for any duplicate emails on Contacts. While writing the test I thought I'd covered everything the original class does however I'm only ending up with 85% code coverage. I'd like to figure out how to get to 100. 

Class (the lines that aren't covered are bolded and underlined):
 
public class Contact_CheckEmail {

    List<Contact> contacts = new List<Contact>();
    
    public Contact_CheckEmail(List<Contact> trigCon){
       contacts = trigCon; 
    }
    

    public void checkEmail(){
    Set<String> emailSet = new Set<String>();
    
    for(Contact c : contacts){
        
        emailSet.add(c.Email);
    
    }
    
    List<Contact> duplicateEmail = [SELECT Email FROM Contact WHERE Email IN: emailSet];
    
    Set<String> duplicateEmailId = new Set<String>();
    
    for(Contact c : duplicateEmail) {
    
        duplicateEmailId.add(c.Email);
    
    }

    for(Contact c: contacts){
        if(duplicateEmailId.contains(c.Email)){
               c.addError('Email already exists.');
        
        }
    
    }
    
}
}


Test Class:
@isTest
public class Test_CheckEmail {
    
    @isTest static void EmailCheckTest(){
        
        Account acc = new Account();
        acc.Name = 'TestAccount';
        insert acc;
        
        List<Contact> contacts = new List<Contact>();
        for( Integer i = 0;i < 1; i++){
            String iString = String.valueof(i);
            Contact myCon = new Contact();
            myCon.LastName = 'iString';
            myCon.AccountId = acc.Id;
            myCon.Email = 'fakeman@fake.com';
            contacts.add(myCon);
        }
        
        try{
        insert contacts;
        }
        catch (Exception e){
            Boolean expectedException = e.getMessage().contains('Email already exists.') ? true : false;
            System.assertEquals(expectedException, true);
        }
        
        List<Contact> contacts2 = new List<Contact>();
        for( Integer i = 0;i < 1; i++){
            String iString = String.valueof(i);
            Contact myCon = new Contact();
            myCon.LastName = 'iString';
            myCon.AccountId = acc.Id;
            myCon.Email = iString + '@fake.com';
            contacts2.add(myCon);
        }
        
        try{
        insert contacts2;
        }
        catch (Exception e){
            Boolean expectedException = e.getMessage().contains('Email already exists.') ? true : false;
            System.assertEquals(expectedException, false);
        }
        
        
        
    }

}

 
Best Answer chosen by Joe Rodden 7
Khan AnasKhan Anas (Salesforce Developers) 
Hi Joe,

Greetings to you!

Please use the below code, I have checked in my org and it is covering 100%
 
@isTest
public class Test_CheckEmail {
    
    @isTest static void EmailCheckTest(){        
        Account acc = new Account();
        acc.Name = 'TestAccount';
        insert acc;
        
        List<Contact> contacts = new List<Contact>();
        for( Integer i = 0;i < 1; i++){
            String iString = String.valueof(i);
            Contact myCon = new Contact();
            myCon.LastName = 'iString';
            myCon.AccountId = acc.Id;
            myCon.Email = 'fakeman@fake.com';
            contacts.add(myCon);
        }
        
        try{
            insert contacts;
        }
        catch (Exception e){
            Boolean expectedException = e.getMessage().contains('Email already exists.') ? true : false;
            System.assertEquals(expectedException, true);
        }
        
        List<Contact> contacts2 = new List<Contact>();
        for( Integer i = 0;i < 1; i++){
            String iString = String.valueof(i);
            Contact myCon = new Contact();
            myCon.LastName = 'iString';
            myCon.AccountId = acc.Id;
            myCon.Email = iString + '@fake.com';
            contacts2.add(myCon);
        }
        
        try{
            insert contacts2;
        }
        catch (Exception e){
            Boolean expectedException = e.getMessage().contains('Email already exists.') ? true : false;
            System.assertEquals(expectedException, false);
        }
        
        Contact_CheckEmail cce = new Contact_CheckEmail(contacts2);
        cce.checkEmail();
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Joe,

Greetings to you!

Please use the below code, I have checked in my org and it is covering 100%
 
@isTest
public class Test_CheckEmail {
    
    @isTest static void EmailCheckTest(){        
        Account acc = new Account();
        acc.Name = 'TestAccount';
        insert acc;
        
        List<Contact> contacts = new List<Contact>();
        for( Integer i = 0;i < 1; i++){
            String iString = String.valueof(i);
            Contact myCon = new Contact();
            myCon.LastName = 'iString';
            myCon.AccountId = acc.Id;
            myCon.Email = 'fakeman@fake.com';
            contacts.add(myCon);
        }
        
        try{
            insert contacts;
        }
        catch (Exception e){
            Boolean expectedException = e.getMessage().contains('Email already exists.') ? true : false;
            System.assertEquals(expectedException, true);
        }
        
        List<Contact> contacts2 = new List<Contact>();
        for( Integer i = 0;i < 1; i++){
            String iString = String.valueof(i);
            Contact myCon = new Contact();
            myCon.LastName = 'iString';
            myCon.AccountId = acc.Id;
            myCon.Email = iString + '@fake.com';
            contacts2.add(myCon);
        }
        
        try{
            insert contacts2;
        }
        catch (Exception e){
            Boolean expectedException = e.getMessage().contains('Email already exists.') ? true : false;
            System.assertEquals(expectedException, false);
        }
        
        Contact_CheckEmail cce = new Contact_CheckEmail(contacts2);
        cce.checkEmail();
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Joe Rodden 7Joe Rodden 7
Thank you, that worked. What was I doing wrong? As I'm currently learning would like to know for the future.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Joe,

I'm glad I was able to help.

According to the code which you have shared in your post, everything is correct but you missed these lines of code:
 
Contact_CheckEmail cce = new Contact_CheckEmail(contacts2);
        cce.checkEmail();

You need to create an instance of a class with a proper constructor parameter(s) (if any), then call the method.

Regards,
Khan Anas
Joe Rodden 7Joe Rodden 7
Ahh so in each test class I need to be sure to call the class it's actually testing?