+ Start a Discussion
vfexp41vfexp41 

Test Class

Hi,
  
     I wrote my vf, apex class may i know how to write the test class for bwlow apex class

  
    public class TestMapController2{
   
    public map<string,integer> data {get;set;}
   
    public TestMapController2(){
        data = new map<string,integer>();
       for(Contact acc: [Select Id, Name  from contact])
     {
            integer count = data.get(acc.name);
          
            if(count != null)
                count++;
            else
                count = 1;
            data.put(acc.name, count);
        
          
        }
    }
}
Swati GSwati G
Hi, 

You can write test class as below.

@isTest
public class TestMapController2Test{

  public static testmethod void testConstructor(){
       // Create setup data
       List<Contact> contactList = new List<Contact>();
       contactList.add(new Contact(LastName = 'Test'));
       contactList.add(new Contact(LastName = 'Test'));
       insert contactList;
       Test.startTest();
       TestMapController2 controller = new TestMapController2();
       Test.stopTest();
       // Assert expected result
       System.assertEquals(2, controller.data.get('Test'));
   }
}