0

enter image description hereMy Test class not covering all the code in this class plz help me to resolve this

Here it is my Class

public class emailCon1 {

    public map<string,string> m{set;get;}
    public list<contact> con{set;get;}

    public emailCon1()
    {
    m=new map<string,string>();
    con=[select lastname,email from contact];

         for(contact c:con)
         {
             m.put(c.email,c.lastname);
         }
    }
}

It is my Test Class

@isTest
public class emailCon1_Tc
{

    static testMethod void testMthd()
    {
        map<string,string> m=new  map<string,string>();
            contact c=new contact();
         emailCon1 ec1=new emailCon1();
          c.lastname='YYY';
         c.email='[email protected]';
        m.put(c.email,c.lastname);
    }
}
2
  • what lines are not covered? How much much percent is covered? I little more information would make things easy for us.
    – Mahmood
    Dec 5, 2014 at 13:32
  • Yes,definitely 85% covered,with in for loop code not covered not only this code many times i faced same thing with for loop.any help appreciable..
    – Krish25
    Dec 5, 2014 at 13:38

2 Answers 2

1

The reason you are not getting full test coverage is because you are not inserting the Contact record, so the code would not of been iterating over the for loop in your code.

This code should give you full test coverage:

@isTest
public class emailCon1_Tc
{
    static testMethod void testMthd() {
        String email = '[email protected]';
        String lastName = 'YYY';
        Contact c = new Contact(LastName = lastName, Email = email);
        insert c;
        emailCon1_Tc ec = new emailCon1_Tc();

        System.assertEquals(1, ec.con.size());
        System.assertEquals(c.Id, ec.con[0].Id);

        System.assertEquals(lastName, ec.m.get(email));
    }
}
2
  • Hi BarCotter,Still its have problem its unable to cover for loop code.
    – Krish25
    Dec 5, 2014 at 13:51
  • If the code I posted runs and all the asserts pass then the for loop is definitely being executed. It could be the UI that displays the code coverage that is wrong. I know that UI has been buggy in the past.
    – BarCotter
    Dec 5, 2014 at 13:53
-2

This is the answer for my Question just 'seeAllData' made me to cover 100% code coverage thanks every one for helping me to resolve issue.

Here i covered NumberOfCons Trigger and emailCon1,emailCon classes

@isTest(seeAllData=true)

public class emailCon_Tc

{

  static testMethod void testMthd()

    {
        contact c=new contact();
        map<string,string> m=new  map<string,string>();
        set<string> e=new set<string>();        
        set<string> n=new set<string>();
         emailCon1 ec1=new emailCon1();
         emailCon ec=new emailCon();  
         c.lastname='YYY';
         c.email='[email protected]';
         insert c;

         c.lastname='YYYY';
         update c;
         delete c;
    }
}
1
  • SeeAllData=true is not best practice and should only be used for special cases such as needing an sObject that can't be created via a testmethod such as ContentWorkspaces or DatedConversionRate - otherwise, testemthods should always create their own testdata
    – cropredy
    Dec 5, 2014 at 18:39

You must log in to answer this question.

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