[SalesForce] Need help for custom controller test class

i implemented chatter check in for this blog:
http://intmist.wordpress.com/2014/03/23/show-custom-visualforce-page-in-salesforce1-setup-publisher-action-and-check-in-using-salesforce1-app/

do some customization in it class to related the check in to sf account object. my class looks like:

public with sharing class AccountCheckInController2 {

public String geolocation { get; set; }
public String text { get; set; }
public String error { get; set; }
public Id accid = ApexPages.CurrentPage().getparameters().get('id');

public void checkin() 
{ 
    // Check if geolocation found or not
 if(geolocation==null || geolocation.length()<8)
 {
    error = 'Unable to retreive location';
    return;
 }

 if(text == null || text.trim().length()==0)
 {
    error = 'Nothing to post';
    return;
 }

 try
 {
    // Post to chatter of current user
    FeedItem post = new FeedItem();
    post.ParentId = Userinfo.getUserId();
    post.Body = text;
    post.ParentId = accid;
    post.Type = 'LinkPost';
    post.LinkUrl = 'http://maps.google.com/maps?q=' + geolocation;
    insert post;
    error = 'Posted Successfully !';

    //post to account activity history
    Task myTask = new Task();
    myTask.WhatId = accid;
    myTask.OwnerId= UserInfo.GetUserId();
    myTask.Status = 'Completed';
    myTask.Subject = 'On-Site Check In';
    myTask.Description = text +'  - http://maps.google.com/maps?q='+geolocation;
    myTask.Priority = 'Normal';
    myTask.ActivityDate = date.today();
    insert myTask;
 }
 catch(Exception ex)
 {
    error = ex.getMessage();
 }
    //return null;
}
}

and my current test class looks like this:

@isTest
private class AccountCheckInTestClass {

static testMethod void myUnitTest() 
{

    // insert test account      
    Account testaccount = new Account(name = 'pt Abc', AccountNumber='123456'); 
    insert testaccount;
    AccountCheckInController2 controller = new AccountCheckInController2();

    //Test.startTest();
    PageReference ref = Page.AccountCheckin2; 
    ApexPages.currentPage().getParameters().put('id', testaccount.Id);
    ApexPages.currentPage().getParameters().put('geolocation','-6.228693,106.8248041');
    ApexPages.currentPage().getParameters().put('text','test checkin');
    Test.setCurrentPage(ref);
    //Test.stopTest();

    controller.checkin();

}
}

from my test class, i just got covered 25% of my code.
it stuck until:

    // Check if geolocation found or not
 if(geolocation==null || geolocation.length()<8)
 {
    error = 'Unable to retreive location';
    return;
 }

after that it not got covered.
and i can't figure it out what wrong with my test class.

regards,

Willy

Best Answer

Instead of

ApexPages.currentPage().getParameters().put('geolocation','-6.228693,106.8248041');

try

controller.geolocation = '-6.228693,106.8248041';

and similar for the other properties of the class. I think the way you're doing it now is not making the data available to your controller.

Related Topic