You need to sign in to do that
Don't have an account?
Joanne Butterfield
Help increasing test code coverage
Hi All,
I am not a developer and having difficulties on writing this test code. I have gotten some help to get this far, but can't get my code coverage over 17%. I have been advised i need system asserts but I'm lost. Any help would be much appreciated.
Thank you in advance! I'll get you beer at Dreamforce :)
Current Test Code
Apex Controller
I am not a developer and having difficulties on writing this test code. I have gotten some help to get this far, but can't get my code coverage over 17%. I have been advised i need system asserts but I'm lost. Any help would be much appreciated.
Thank you in advance! I'll get you beer at Dreamforce :)
Current Test Code
@ isTest private class TestCaseTravel { static testMethod void testtravelController() { // Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; User s = new User( Alias = 'standt', Email='standarduser@seon.com.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', Division = 'School Bus', UserName='standarduser@seon.com'); insert s; User t = new User( Alias = 'standt', Email='standardusert@seon.com.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', Division = 'Transit', UserName='standardusert@seon.com'); insert t; User o = new User( Alias = 'standt', Email='standarduser0@seon.com.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', Division = 'Other', UserName='standarduser0@seon.com'); insert o; /** * Create dummy objects for testing. Might need to add missing required fields */ List<Travel__c> travels = new List<Travel__c>{}; new Travel__c ( Name='Test Travel' ,Departure_Date__c = Date.today() ,Return_Date__c = Date.today() ,Approval_Status__c = 'Approved' ,Travel_Purpose__c = 'test' ) ;new Travel__c( Name='Test Travel 2' ,Departure_Date__c = Date.today() ,Return_Date__c = Date.today() ,Approval_Status__c = 'Approved' ,Travel_Purpose__c = 'test' ,OwnerId = s.Id ) ;new Travel__c( Name='Test Travel 3' ,Departure_Date__c = Date.today() ,Return_Date__c = Date.today() ,Approval_Status__c = 'Approved' ,Travel_Purpose__c = 'test' ,OwnerId = t.Id ) ;new Travel__c( Name='Test Travel 4' ,Departure_Date__c = Date.today() ,Return_Date__c = Date.today() ,Approval_Status__c = 'Approved' ,Travel_Purpose__c = 'test' ,OwnerId = o.Id ) ; insert travels; Event e = new Event(); e.Subject = 'Test Event'; e.Location = 'Test Location'; e.Description = 'Test Description'; e.StartDateTime = Datetime.now(); e.EndDateTime = Datetime.now(); insert e; Test.startTest(); TravelCalendar_Controller controller = new TravelCalendar_Controller(); controller.pageLoad(); Test.stopTest(); //System.assertEquals(someValue,controller.someProperty); //System.assertNotEquals(true,controller.events.isEmpty()); //System.assertEquals(someValue,controller.events.get(0).someProperty); //System.assertEquals(false,controller.includeMyEvents); } }
Apex Controller
public class TravelCalendar_Controller { public Boolean includeMyEvents {get;set;} public list<calEvent> events {get;set;} //The calendar plugin is expecting dates is a certain format. We can use this string to get it formated correctly String dtFormat = 'EEE, d MMM yyyy HH:mm:ss z'; //constructor public TravelCalendar_Controller() { //Default showing my events to on includeMyEvents = false; } public PageReference pageLoad() { events = new list<calEvent>(); //Get My Travel for(Travel__c mytrav : [select Id, Owner.FirstName, Owner.LastName, Name, Departure_Date__c, Return_Date__c, Owner_Territory__c from Travel__c where Approval_Status__c = 'Approved' and OwnerID = :UserInfo.getUserId() ]){ DateTime startDT = myTrav.Departure_Date__c; DateTime endDT = myTrav.Return_Date__c; calEvent mytravEvent = new calEvent(); mytravEvent.title = 'Trip:'+ ' ' + myTrav.Name+ ' ' + myTrav.Owner.FirstName+ ' ' + myTrav.Owner.LastName; mytravEvent.allDay = true; mytravEvent.startString = startDT.format(dtFormat); mytravEvent.endString = endDT.format(dtFormat); mytravEvent.url = '/' + myTrav.Id; mytravEvent.className = 'event-mytravel'; events.add(mytravEvent); } //Get School Bus Travel for(Travel__c sbtrav : [select Id, Owner.FirstName, Owner.LastName, Name, Departure_Date__c, Return_Date__c, Owner_Territory__c from Travel__c where Approval_Status__c = 'Approved' and Owner_Division__c = 'School Bus' and OwnerID != :UserInfo.getUserId() ]){ DateTime startDT = sbTrav.Departure_Date__c; DateTime endDT = sbTrav.Return_Date__c; calEvent sbtravEvent = new calEvent(); sbtravEvent.title = 'Trip:'+ ' ' + sbTrav.Name+ ' ' + sbTrav.Owner.FirstName+ ' ' + sbTrav.Owner.LastName; sbtravEvent.allDay = true; sbtravEvent.startString = startDT.format(dtFormat); sbtravEvent.endString = endDT.format(dtFormat); sbtravEvent.url = '/' + sbTrav.Id; sbtravEvent.className = 'event-sbtravel'; events.add(sbtravEvent); } //Get Transit Travel for(Travel__c trtrav : [select Id, Owner.FirstName, Owner.LastName, Name, Departure_Date__c, Return_Date__c, Owner_Territory__c from Travel__c where Approval_Status__c = 'Approved' and Owner_Division__c = 'Transit' and OwnerID != :UserInfo.getUserId() ]){ DateTime startDT = trTrav.Departure_Date__c; DateTime endDT = trTrav.Return_Date__c; calEvent trtravEvent = new calEvent(); trtravEvent.title = 'Trip:'+ ' ' + trTrav.Name+ ' ' + trTrav.Owner.FirstName+ ' ' + trTrav.Owner.LastName; trtravEvent.allDay = true; trtravEvent.startString = startDT.format(dtFormat); trtravEvent.endString = endDT.format(dtFormat); trtravEvent.url = '/' + trTrav.Id; trtravEvent.className = 'event-trtravel'; events.add(trtravEvent); } //Get Other Travel for(Travel__c ottrav : [select Id, Owner.FirstName, Owner.LastName, Name, Departure_Date__c, Return_Date__c, Owner_Territory__c from Travel__c where Approval_Status__c = 'Approved' and Owner_Division__c != 'School Bus' and Owner_Division__c != 'Transit'and OwnerID != :UserInfo.getUserId() ]){ DateTime startDT = otTrav.Departure_Date__c; DateTime endDT = otTrav.Return_Date__c; calEvent ottravEvent = new calEvent(); ottravEvent.title = 'Trip:'+ ' ' + otTrav.Name+ ' ' + otTrav.Owner.FirstName+ ' ' + otTrav.Owner.LastName; ottravEvent.allDay = true; ottravEvent.startString = startDT.format(dtFormat); ottravEvent.endString = endDT.format(dtFormat); ottravEvent.url = '/' + otTrav.Id; ottravEvent.className = 'event-ottravel'; events.add(ottravEvent); } //Get my Events if we have selected the correct option if(includeMyEvents){ for(Event evnt: [select Id, Subject, isAllDayEvent, StartDateTime, EndDateTime from Event where OwnerID = :UserInfo.getUserId()]){ DateTime startDT = evnt.StartDateTime; DateTime endDT = evnt.EndDateTime; calEvent myEvent = new calEvent(); myEvent.title = evnt.Subject; myEvent.allDay = evnt.isAllDayEvent; myEvent.startString = startDT.format(dtFormat); myEvent.endString = endDT.format(dtFormat); myEvent.url = '/' + evnt.Id; myEvent.className = 'event-personal'; events.add(myEvent); } } return null; } public PageReference toggleMyEvents() { if(includeMyEvents){ includeMyEvents = false; } else{ includeMyEvents = true; } pageload(); return null; } //Class to hold calendar event data public class calEvent{ public String title {get;set;} public Boolean allDay {get;set;} public String startString {get;private set;} public String endString {get;private set;} public String url {get;set;} public String className {get;set;} } }VF Page
<apex:page controller="TravelCalendar_Controller" action="{!pageLoad}"> <apex:relatedList list="TravelCalendar_Controller" /> <link href="{!$Resource.fullCalendarCSS}" rel="stylesheet" /> <link href="{!$Resource.fullCalendarPrintCSS}" rel="stylesheet" media="print" /> <apex:includeScript value="{!$Resource.moment_min_js}" /> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script> <script src="{!$Resource.fullCalendarMinJS}"></script> <script> //We need to wrap everything in a doc.ready function so that the code fires after the DOM is loaded $(document).ready(function() { //Call the fullCallendar method. You can replace the '#calendar' with the ID of the dom element where you want the calendar to go. $('#calendar').fullCalendar({ header: { left: 'prev,next today', center: 'title', right: 'month,agendaWeek,agendaDay' }, editable: false, events: [ //At run time, this APEX Repeat will reneder the array elements for the events array <apex:repeat value="{!events}" var="e"> { title: "{!e.title}", start: '{!e.startString}', end: '{!e.endString}', url: '{!e.url}', allDay: {!e.allDay}, className: '{!e.className}' }, </apex:repeat> ] }); }); </script> <!--some styling. Modify this to fit your needs--> <style> #cal-options {float:left;} #cal-legend { float:right;} #cal-legend ul {margin:0;padding:0;list-style:none;} #cal-legend ul li {margin:0;padding:5px;float:left;} #cal-legend ul li span {display:block; height:16px; width:16px; margin-right:4px; float:left; border-radius:4px;} #calendar {margin-top:20px;} #calendar a:hover {color:#fff !important;} .fc-event-inner {padding:3px;} .event-personal {background:#1797c0;border-color:#1797c0;} .event-mytravel {background:#00447b;border-color:#00447b;} .event-sbtravel {background:#FCB514;border-color:#FCB514;} .event-trtravel {background:#00A5DB;border-color:#00A5DB;} .event-ottravel {background:#676C74;border-color:#676C74;} </style> <apex:sectionHeader title="Approved Travel Calendar"/> <apex:outputPanel id="calPanel"> <apex:form > <div id="cal-options"> <apex:commandButton value="{!IF(includeMyEvents,'Hide My Events','Show My Events')}" action="{!toggleMyEvents}"/> </div> <div id="cal-legend"> <ul> <li><span class="event-mytravel"></span>My Travel</li> <li><span class="event-sbtravel"></span>School Bus Travel</li> <li><span class="event-trtravel"></span>Transit Travel</li> <li><span class="event-ottravel"></span>Other Travel</li> <li style="{!IF(includeMyEvents,'','display:none')}"><span class="event-personal"></span>My Events</li> </ul> <div style="clear:both;"><!--fix floats--></div> </div> <div style="clear:both;"><!--fix floats--></div> <div id="calendar"></div> </apex:form> </apex:outputPanel> </apex:page>Calendar
Please replace you following code where you are inserting Travel with below code
You haev inserted 4 Travel records, but you have not mentioned the
Owner_Division__c field there..
Pleae replace the code with this for only list and check and let me know if it helps you to increase the coverage..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
I get an error when I add owner division to the travel records, this field is a formula which produces the division from the owner user record.
Any other ideas? Thanks again!
If this field is a formula field, then you need to create user record and insert Division.
Hence it will help to cover that also.
Please let me know if you need any other help.
What is Test Coverage? Test coverage measures how much of your application you test. ...
#1 Create a Plan and Set a Goal. ...
#2 Increase Code Coverage. ...
#3 Enhance Test (https://www.asiaheavens.com/%d8%b9%d8%b1%d9%88%d8%b6-%d8%b3%d9%81%d8%b1-%d8%a7%d9%88%d9%83%d8%b1%d8%a7%d9%86%d9%8a%d8%a7/) Automation. ...
#4 Test on a Larger Scale. ...
Bonus: Increase Test Coverage by Running Tests with a Cloud Test Service.