0

I have a visualforce page with a drag and drop calendar based on: https://salesforceprojects.quora.com/Implement-full-calendar-drag-and-drop-in-Visualforce-page

I have started to write my test classes. The values of two strings are set on the visualforce page in apex: param Values. When running my test these values are returning null. I thought I could mimic this in my test class by using a getParamerters().put() in my test class, however when running my test these are still being passed into the method as null?


I am having trouble testing the setEvent method in the Extension:

public class CalendarControllerExtension {

    private final SalesActivityPlan__c sap; 
    public list <CalendarEvent> events {get;set;}    

    // As Events can be dynamically dragged around the calender variables are used for these selections
    public String eventIdTemp   {get;set;}  //Selected event Id coming from vf page
    public String st    {get;set;}  //Selected event start date & time
    public String ed    {get;set;} //Selected event end date & time
    public List <Event> eventList = new List <Event> ();

    //Do some stuff

//   Parse the date format and set values when an event is dragged around the calendar
    public void setEvent() {
        if (ed != null & st !=null){
        List <String> stringParts = ed.split('T');
        List <String> dateParts = stringParts[0].split('-');
        List <String> timeParts = stringParts[1].split(':');  
        DateTime dt1 = DateTime.newInstance(Integer.valueOf(dateParts[0]), Integer.valueOf(dateParts[1]), Integer.valueOf(dateParts[2]), Integer.valueOf(timeParts[0]), Integer.valueOf(timeParts[1]), Integer.valueOf(timeParts[2]));
        stringParts = st.split('T');
        dateParts = stringParts[0].split('-');
        timeParts = stringParts[1].split(':');
        DateTime dt2 =  DateTime.newInstance(Integer.valueOf(dateParts[0]), Integer.valueOf(dateParts[1]), Integer.valueOf(dateParts[2]), Integer.valueOf(timeParts[0]), Integer.valueOf(timeParts[1]), Integer.valueOf(timeParts[2]));
        for(Event e : eventList) {  
            if(e.Id == eventIdTemp) { 
                e.EndDateTime = dt1;
                e.StartDateTime = dt2;
                update e; }
            }
        }
    }
}

Test Class:

@isTest

public class ActivityPlanExtensionTest {

@isTest    

static void TestActivityPlanExtension(){

    User u3 = [SELECT Id FROM User WHERE Email='test@test.com'];

    Event TestEvent = new Event(Subject = 'Test', ActivityDate=date.today(), StartDateTime=datetime.now(), EndDateTime=datetime.now());
    insert testevent;

    String ed = string.valueOfGmt(testevent.enddatetime);
    String st = string.valueOfGmt(testevent.startdatetime);
    String eventIdTemp = string.valueof(testevent.id);


    SalesActivityPlan__c testplan = new SalesActivityPlan__c();

    //Instantiate VF Page
    PageReference pg = Page.ActivityPlan;   
    Test.setCurrentPage(pg);
    ApexPages.currentPage().getParameters().put('st', string.valueOfGmt(testevent.startdatetime));
    ApexPages.currentPage().getParameters().put('ed', string.valueOfGmt(testevent.enddatetime));
    system.debug(st);
    system.debug(ed);

    // Instantiate the controller extensions and run the methods
    ApexPages.StandardController sc = new ApexPages.standardController(testplan);   
    ActivityPlanExtension actplan = new ActivityPlanExtension(sc);
    CalendarControllerExtension calendarplan = new  CalendarControllerExtension(sc);
        calendarplan.pageLoad();
        st = ApexPages.CurrentPage().GetParameters().Get('st');
        ed = ApexPages.CurrentPage().GetParameters().Get('ed');
        calendarplan.setEvent();

}
}
7
  • These comments in your code are excessive and make it much harder to read. – Adrian Larson Apr 10 '17 at 18:06
  • 1
    Can you try like this : calendarplan.st = string.valueOfGmt(testevent.startdatetime); ? – javanoob Apr 10 '17 at 18:16
  • 1
    I've taken out a good portion of the code. Hopefully it is more readable. – Starley Flynn Apr 10 '17 at 18:23
  • 2
    Did you mean to set calendarplan.st and calnedarplan.ed instead of st and ed? You'll have a much easier time working with this code if you work on sensible naming (for instance name your extension instance extension and your mental paradigm will be easier to grasp as you code). – Adrian Larson Apr 10 '17 at 18:31
  • 2
    I don't see anywhere in your controller where you are "getting" the page parameters so what's the point? Just set st and ed directly – Eric Apr 10 '17 at 18:53
0

Credit to javanoob, Adrian Larson and Eric.

set calendarplan.st and calnedarplan.ed works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.