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
Laytro80Laytro80 

Testing Coverage Help

Hi,

 

Am still a newbie on APEX and testing scripts.  The APEX code itself works exactly right.  The Testing script contained is successful with 0% coverage.

 

What the controller does

 

We have an object called meeting note, and an object called attendee.  Attendee appears as a related list on a new meeting note. When a user clicks new a visualforce page loads with a very simple input form.  When the user hits save they are returned back to the meeting note note the saved attendee record.

 

Any pointers / help on the test script would be much appreciated.

 

 

public class attendeeExt {

    Attendee__c attendee;

    public attendeeExt(ApexPages.StandardController ctlr){
        
        this.attendee = (Attendee__c)ctlr.getRecord();
    }   
    
    public Attendee__c getAttendee(){
        if(attendee == null) attendee = new Attendee__c();
        return attendee;
    }    

    public PageReference Save(){
        try{
            insert attendee;
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);
        } 
        PageReference pr = new PageReference('/'+attendee.Meeting_Note__c);
        pr.setRedirect(True);
            return pr;
    }
    public static testMethod void testattendeeExt() {        
        Contact c = new Contact(FirstName='Test', LastName='Contact');
        insert c;
        
        Meeting_Note__c m = new Meeting_Note__c(Subject__c='Test');
        insert m;
        
        Attendee__c a = new Attendee__c(Meeting_Note__c = m.id, Contact__c = c.id);
        insert a;
                
        System.assertEquals(c.id,a.contact__c);
    }
}

 

 

Thanks

 

Ross

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SargeSarge

Hi Ross,

 

    Why cant the get method be called in test method? Actually in action, this method is automatically called by VF when the page loads. We can simulate it here. Hence we need to address it manually for test coverage purpose. Below might be the unit test code.

 

public static testMethod void testattendeeExt() {
    Contact c = new Contact(FirstName='Test', LastName='Contact');
    insert c;
   
    Meeting_Note__c m = new Meeting_Note__c(Subject__c='Test');
    insert m;
   
    Attendee__c a = new Attendee__c(Meeting_Note__c = m.id, Contact__c = c.id);
    insert a;
   
    System.assertEquals(c.id,a.contact__c);
   
    ApexPages.StandardController sc = new ApexPages.StandardController(a);
    attendeeext ae = new attendeeext(sc);
    Attendee__c attTemp = ae.attendee;
    ae.attendee = null;
    ae.getAttendee();

    ae.attendee = attTemp;
    ae.save();
}

All Answers

hisrinuhisrinu
public class attendeeExt {

    Attendee__c attendee;

    public attendeeExt(ApexPages.StandardController ctlr){
        
        this.attendee = (Attendee__c)ctlr.getRecord();
    }   
    
    public Attendee__c getAttendee(){
        if(attendee == null) attendee = new Attendee__c();
        return attendee;
    }    

    public PageReference Save(){
        try{
            insert attendee;
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);
        } 
        PageReference pr = new PageReference('/'+attendee.Meeting_Note__c);
        pr.setRedirect(True);
            return pr;
    }
    public static testMethod void testattendeeExt() {        
        Contact c = new Contact(FirstName='Test', LastName='Contact');
        insert c;
        
        Meeting_Note__c m = new Meeting_Note__c(Subject__c='Test');
        insert m;
        
        Attendee__c a = new Attendee__c(Meeting_Note__c = m.id, Contact__c = c.id);
        insert a;
                
        System.assertEquals(c.id,a.contact__c);

        ApexPages.StandardController sc = new ApexPages.StandardController(a);
        attendeeext a = new attendeeext(sc);
        a.save();

  } }
Laytro80Laytro80

Thanks for this I get an error.

 

ErrorError: Compile Error: Duplicate variable: a (attempt to re-create the variable with type: attendeeext) at line 39 column 21
Laytro80Laytro80

Sorry spotted the error the a variable was being used twice.

 

Thanks for your help.

 

 

Laytro80Laytro80

Need to get increased code coverage.  I currently have 73%.  The lines highlight in red seem to be the problem.

 

 

public class attendeeExt {
 2	 	  
 3	 1	   Attendee__c attendee;
 4	 	  
 5	 1	   public attendeeExt(ApexPages.StandardController ctlr){
 6	 	  
 7	 1	   this.attendee = (Attendee__c)ctlr.getRecord();
 8	 	   }
 9	 	  
 10	 0	   public Attendee__c getAttendee(){
 11	 0	   if(attendee == null) attendee = new Attendee__c();
 12	 0	   return attendee;
 13	 	   }
 14	 	  
 15	 1	   public PageReference Save(){
 16	 1	   try{
 17	 1	   insert attendee;
 18	 	   }
 19	 1	   catch(DmlException ex){
 20	 1	   ApexPages.addMessages(ex);
 21	 	   }
 22	 1	   PageReference pr = new PageReference('/'+attendee.Meeting_Note__c);
 23	 1	   pr.setRedirect(True);
 24	 1	   return pr;
 25	 	   }
 26	 	   public static testMethod void testattendeeExt() {
 27	 	   Contact c = new Contact(FirstName='Test', LastName='Contact');
 28	 	   insert c;
 29	 	  
 30	 	   Meeting_Note__c m = new Meeting_Note__c(Subject__c='Test');
 31	 	   insert m;
 32	 	  
 33	 	   Attendee__c a = new Attendee__c(Meeting_Note__c = m.id, Contact__c = c.id);
 34	 	   insert a;
 35	 	  
 36	 	   System.assertEquals(c.id,a.contact__c);
 37	 	  
 38	 	   ApexPages.StandardController sc = new ApexPages.StandardController(a);
 39	 	   attendeeext ae = new attendeeext(sc);
 40	 	   ae.save();
 41	 	   }
 42	 	  }

 

 

hisrinuhisrinu
public class attendeeExt {
 2	 	  
 3	 1	   Attendee__c attendee;
 4	 	  
 5	 1	   public attendeeExt(ApexPages.StandardController ctlr){
 6	 	  
 7	 1	   this.attendee = (Attendee__c)ctlr.getRecord();
 8	 	   }
 9	 	  
 10	 0	   public Attendee__c getAttendee(){
 11	 0	   if(attendee == null) attendee = new Attendee__c();
 12	 0	   return attendee;
 13	 	   }
 14	 	  
 15	 1	   public PageReference Save(){
 16	 1	   try{
 17	 1	   insert attendee;
 18	 	   }
 19	 1	   catch(DmlException ex){
 20	 1	   ApexPages.addMessages(ex);
 21	 	   }
 22	 1	   PageReference pr = new PageReference('/'+attendee.Meeting_Note__c);
 23	 1	   pr.setRedirect(True);
 24	 1	   return pr;
 25	 	   }
 26	 	   public static testMethod void testattendeeExt() {
 27	 	   Contact c = new Contact(FirstName='Test', LastName='Contact');
 28	 	   insert c;
 29	 	  
 30	 	   Meeting_Note__c m = new Meeting_Note__c(Subject__c='Test');
 31	 	   insert m;
 32	 	  
 33	 	   Attendee__c a; 
35 36 System.assertEquals(c.id,a.contact__c); 37 38 ApexPages.StandardController sc = new ApexPages.StandardController(a); 39 attendeeext ae = new attendeeext(sc); 40 ae.save(); 41 } 42 }
Laytro80Laytro80

Thanks,

 

Still only getting 73% test coverage line 37 was added I had to change a to a different variable.

 

 

hisrinuhisrinu

Why dont you remove the assertion and then run test... let me know which lines were not covered

Laytro80Laytro80

Thanks for all your help I removed line 

 

System.assertEquals(c.id,a.contact__c);

The problem lines are:

 

 

 10      0	   public Attendee__c getAttendee(){
 11	 0	   if(attendee == null) attendee = new Attendee__c();
 12	 0	   return attendee;
 13	 	   }

 

 

 

Laytro80Laytro80

Hi,

 

Sorry to ask, but did you have any other suggestions?

 

Thanks

 

Ross

SargeSarge

Hi Ross,

 

    Why cant the get method be called in test method? Actually in action, this method is automatically called by VF when the page loads. We can simulate it here. Hence we need to address it manually for test coverage purpose. Below might be the unit test code.

 

public static testMethod void testattendeeExt() {
    Contact c = new Contact(FirstName='Test', LastName='Contact');
    insert c;
   
    Meeting_Note__c m = new Meeting_Note__c(Subject__c='Test');
    insert m;
   
    Attendee__c a = new Attendee__c(Meeting_Note__c = m.id, Contact__c = c.id);
    insert a;
   
    System.assertEquals(c.id,a.contact__c);
   
    ApexPages.StandardController sc = new ApexPages.StandardController(a);
    attendeeext ae = new attendeeext(sc);
    Attendee__c attTemp = ae.attendee;
    ae.attendee = null;
    ae.getAttendee();

    ae.attendee = attTemp;
    ae.save();
}

This was selected as the best answer
Laytro80Laytro80

Thanks so much I did not realise you needed to manually test the get method.

 

Much appreciated.

 

Kind regards

 

Ross

Laytro80Laytro80

Thanks for all your help Srini