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 

Urgent help needed with test coverage

Need to get increased code coverage.  I currently have 73%.  The lines highlight in red seem to be the problem.  I have received help to get to this point but I not sure of the code required to test the missing lines or how to get this above the required 75%.
Thanks
Ross

 

 

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	 	  }

 

 

bbrantly1bbrantly1

It doesn't look like you're calling that function in the test method, try the following:

 

First, change

Attendee__c attendee;

 

 

To

 

public Attendee__c attendee;

 

Then in your Test Method try:

 

/*Current Line*/
ApexPages.StandardController sc = new ApexPages.StandardController(a);



/****** New Lines Start ******/
sc.attendee = null // Set to Null so your if statement will run
Attendee__c AttendeeObjOutput = sc.getAttendee(); //Run Function, since attendee is null it should execute each line of the if statement
/****** New Lines End *******/


/*Current Lines*/
attendeeext ae = new attendeeext(sc);
ae.save();

 

 

Let me know if this doesn't work and we can try a couple of other things.

 

Laytro80Laytro80

Thanks so much for your help.  I got there in the end with the code you sent the trigger for but the changes you made seemed better than my solution so thanks.

 

Is it possible to point me in the right direction on this one, I have started to write the test script but only get 40% coverage.

 

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

 

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 and 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();
}

}