You need to sign in to do that
Don't have an account?
Joe 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):
Test Class:
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); } } }
Greetings to you!
Please use the below code, I have checked in my org and it is covering 100%
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
Greetings to you!
Please use the below code, I have checked in my org and it is covering 100%
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
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:
You need to create an instance of a class with a proper constructor parameter(s) (if any), then call the method.
Regards,
Khan Anas