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 (again!)

I have started to write the test script but only get 40% coverage, very new to APEX but slowly getting there with a lot of help.  Thanks to all the community for your support!

 

The code below is simple it just allows a meeting note and attendee object to sit on the same page (visualforce) and when you hit save a meeting note record is created and attendee is linked to the meeting note. 

 

I am sure the testing code is poor to say the least but I am very new to all this and having to rely on free stuff as I can't afford the developer courses yet.

 

Thanks

 

Ross

 

 

public class newMeetingController {

    public newMeetingController(ApexPages.StandardController controller) {
    }

   Meeting_Note__c meeting;
   Attendee__c attendee;
    
   public Meeting_Note__c getMeeting() {
      if(Meeting == null) Meeting = new Meeting_Note__c();
      return Meeting;
   }

   public Attendee__c getAttendee() {
      if(Attendee == null) Attendee = new Attendee__c();
      return Attendee;
   } 
    
    public PageReference cancel() {
            PageReference MeetPage = new ApexPages.StandardController(Meeting).view();
            MeetPage.setRedirect(true);
            return MeetPage; 
    }
    
   public PageReference save() {

      insert meeting; 
    
      attendee.meeting_note__c = meeting.id;
      insert attendee;

      PageReference meetPage = new ApexPages.StandardController(Meeting).view();
      meetPage.setRedirect(true);

      return meetPage;
   }
   
    public static testMethod void newMeetingController() {
    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);
    newMeetingController ae = new newMeetingController(sc);
    Attendee__c attTemp = ae.attendee;
    ae.attendee = null;
    ae.getAttendee();
    ae.attendee = attTemp;
    ae.save();
    
    newMeetingController me = new newMeetingController(sc);
    Meeting_Note__c meTemp = me.meeting;
    me.meeting = null;
    me.getMeeting();
    me.Meeting = meTemp;
    me.save();
}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

 

Always a bit tricky to be sure this will work without being able to compile it, but I think this will get you started - you'll also need to write some code to assert the records are created etc...

 

public static testMethod void testMeetingController() {

//setup test data  
    Contact c = new Contact(FirstName='Test', LastName='Contact');
    insert c;
   
//test main controller action     
newMeetingController nmc new newMeetingController();
nmc.getMeeting();
nmc.getAttendee();

nmc.attendee.Contact__c = c.Id;
nmc.save();

//you'll need do some system asserts now, to check Meeting created, and attendee linked with correct contact

//also, test cancel button
newMeetingController nmc1 new newMeetingController();
nmc1.getMeeting();
nmc1.getAttendee();

nmc1.cancel();
//you can use getURL() function to check next page is correct - take a look in the help for examples
    
 
     }

 

 

All Answers

benwrigleybenwrigley

Coverage looks like it should be much higher that 40%. are you using the Force.com IDE / eclipse because it will report exactly which lines are not covered.

BritishBoyinDCBritishBoyinDC

I don't think you ever call the cancel() method, so that is a good chunk of the missing code I suspect.

 

You can also run the test in standard Salesforce UI, and view the results - there is nice color coded output that shows which code is covered/not covered - that should steer you in the right direction as well.

Laytro80Laytro80

Hi,

 

Thanks for your help so far.  Below is the testing script, coverage is 40% the 1 lines are ok, the 0 lines are a problem.

 

newMeetingController (Code Covered: 40%)
 line	  executions	 source
 1	 	  public class newMeetingController {
 2	 	  
 3	 1	   public newMeetingController(ApexPages.StandardController controller) {
 4	 	   }
 5	 	  
 6	 1	   Meeting_Note__c meeting;
 7	 1	   Attendee__c attendee;
 8	 	  
 9	 0	   public Meeting_Note__c getMeeting() {
 10	 0	   if(Meeting == null) Meeting = new Meeting_Note__c();
 11	 0	   return Meeting;
 12	 	   }
 13	 	  
 14	 1	   public Attendee__c getAttendee() {
 15	 1	   if(Attendee == null) Attendee = new Attendee__c();
 16	 1	   return Attendee;
 17	 	   }
 18	 	  
 19	 0	   public PageReference cancel() {
 20	 0	   PageReference MeetPage = new ApexPages.StandardController(Meeting).view();
 21	 0	   MeetPage.setRedirect(true);
 22	 0	   return MeetPage;
 23	 	   }
 24	 	  
 25	 1	   public PageReference save() {
 26	 	  
 27	 1	   insert meeting;
 28	 	  
 29	 0	   attendee.meeting_note__c = meeting.id;
 30	 0	   insert attendee;
 31	 	  
 32	 0	   PageReference meetPage = new ApexPages.StandardController(Meeting).view();
 33	 0	   meetPage.setRedirect(true);
 34	 	  
 35	 0	   return meetPage;
 36	 	   }
 37	 	  
 38	 	   public static testMethod void newMeetingController() {
 39	 	   Contact c = new Contact(FirstName='Test', LastName='Contact');
 40	 	   insert c;
 41	 	  
 42	 	   Meeting_Note__c m = new Meeting_Note__c(Subject__c='Test');
 43	 	   insert m;
 44	 	  
 45	 	   Attendee__c a = new Attendee__c(Meeting_Note__c = m.id, Contact__c = c.id);
 46	 	   insert a;
 47	 	  
 48	 	   System.assertEquals(c.id,a.contact__c);
 49	 	  
 50	 	   ApexPages.StandardController sc = new ApexPages.StandardController(a);
 51	 	   newMeetingController ae = new newMeetingController(sc);
 52	 	   Attendee__c attTemp = ae.attendee;
 53	 	   ae.attendee = null;
 54	 	   ae.getAttendee();
 55	 	   ae.attendee = attTemp;
 56	 	   ae.save();
 57	 	  
 58	 	   newMeetingController me = new newMeetingController(sc);
 59	 	   Meeting_Note__c meTemp = me.meeting;
 60	 	   me.meeting = null;
 61	 	   me.getMeeting();
 62	 	   me.Meeting = meTemp;
 63	 	   me.save();
 64	 	  }
 65	 	  
 66	 	  }

 

benwrigleybenwrigley

Are you sure your insert succeeds? The uncovered lines appear to be methods called after the insert.

 

and as was pointed out, it doesn't look like you call the cancel method.

Laytro80Laytro80

Completely lost....  I have had to make some changes to the VF page, code below, it is much simpler now and most importantly seems to work.

 

<apex:page controller="newMeetingController" tabStyle="Meeting_Note__c">
  <apex:sectionHeader title="Meeting Note"/>
  <apex:form >
    <apex:pageBlock mode="edit">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="Meeting Note Information">
        <apex:inputField id="meetingName" value="{!meeting.Subject__c}" style="width: 50%"/>
        <apex:inputField id="meetingDate" value="{!meeting.Date_of_meeting__c}"/>
        <apex:inputField id="meetingAccount" value="{!meeting.Account__c}"/>
      </apex:pageBlockSection>
      <apex:pageBlockSection title="File Note" columns="1">
        <apex:inputField id="meetingName" value="{!meeting.Full_Note__c}" style="width: 80%"/>
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Primary Attendee" columns="1">
        <apex:inputField id="meetingContact" value="{!attendee.Contact__c}" required="true"/>
      </apex:pageBlockSection>      
    </apex:pageBlock>
  </apex:form>
</apex:page>

Also made some changes to the APEX code, and remove out most of the testing script I now get 0% coverage but all of the inserts are a success.  See code below (have highlighted in bold the test part.  Any snippets of code to point me in the right direction would be very very much appreciated.

 

Cheers

 

R

 

 

public class newMeetingController {

   Meeting_Note__c meeting;
   Attendee__c attendee;

   public Meeting_Note__c getMeeting() {
      if(Meeting == null) Meeting = new Meeting_Note__c();
      return Meeting;
   }

   public Attendee__c getAttendee() {
      if(Attendee == null) Attendee = new Attendee__c();
      return Attendee;
   } 
    
   public PageReference cancel() {
            PageReference MeetPage = new ApexPages.StandardController(Meeting).view();
            MeetPage.setRedirect(true);
            return MeetPage; 
   }
    
   public PageReference save() {

      insert meeting; 
    
      attendee.meeting_note__c = meeting.id;
      attendee.Primary_Contact__c = TRUE;      
      insert attendee;

      PageReference meetPage = new ApexPages.StandardController(Meeting).view();
      meetPage.setRedirect(true);

      return meetPage;
   }
   
    public static testMethod void newMeetingController() {
    
    Meeting_Note__c meeting;
    Attendee__c attendee;
    
    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;               
      
     }
       
       }

 

 

BritishBoyinDCBritishBoyinDC

 

Always a bit tricky to be sure this will work without being able to compile it, but I think this will get you started - you'll also need to write some code to assert the records are created etc...

 

public static testMethod void testMeetingController() {

//setup test data  
    Contact c = new Contact(FirstName='Test', LastName='Contact');
    insert c;
   
//test main controller action     
newMeetingController nmc new newMeetingController();
nmc.getMeeting();
nmc.getAttendee();

nmc.attendee.Contact__c = c.Id;
nmc.save();

//you'll need do some system asserts now, to check Meeting created, and attendee linked with correct contact

//also, test cancel button
newMeetingController nmc1 new newMeetingController();
nmc1.getMeeting();
nmc1.getAttendee();

nmc1.cancel();
//you can use getURL() function to check next page is correct - take a look in the help for examples
    
 
     }

 

 

This was selected as the best answer
Laytro80Laytro80

Thanks for this it's a massive help and I can at-least see the wood for the trees.

 

Much appreciated.

 

Cheers

 

R