I have the following code which is fetching only First Name from Contact where Account Name is 'United Oil & Gas, UK'.

public class ContactUK {    
    public List<string> viewcontactUK(string acctnm){
        List<string> contactFirstName = new List<string>();   
        Set<string> contactset = new set<string>();  
            for (Contact c : [SELECT FirstName,Name FROM Contact where Account.Name = :acctnm]){
                contactFirstName.add (c.FirstName);
        Return contactFirstName;

here is the test class I have written to get 100% test coverage

private class testContactUK {
    static testmethod void myTest(){
        Account acc = new Account(Name = 'Test Account');
        Contact c = new Contact(FirstName = 'Ashley',
                                LastName = 'Jones',
                                AccountId = acc.Id);
        insert c;
        ContactUK cont = new ContactUK();
        cont.viewcontactUK('United Oil & Gas, UK');                             

But the below code is not covered under my test coverage:-

contactFirstName.add (c.FirstName);

Could anybody please help me to understand which test coverage is excluding those two lines of code?

  • You are mixing up Account Names. Either pass Test Account to cont.viewcontactUK() or change your test Account Name to United Oil & Gas, UK.
    – TSmith
    Sep 6, 2021 at 12:34

1 Answer 1

  1. In the test code you are missing to insert the account
  2. You should search by the account name you created, so if you creates 'Test Account' you should pass to the method 'Test Account' as name of the account to search.

Hope this solves your question

  • It worked!!! Thanks a lot.
    – Suvadeep
    Sep 6, 2021 at 12:55

Not the answer you're looking for? Browse other questions tagged .