You need to sign in to do that
Don't have an account?

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(); } }
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...
All Answers
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.
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.
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.
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.
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.
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
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...
Thanks for this it's a massive help and I can at-least see the wood for the trees.
Much appreciated.
Cheers
R