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
Naveen KvpNaveen Kvp 

Test class for costom controller??

I am try to write a test class for custom controller but i got some errors i am not that much familiar with writing test classes here i am post the controller code and test class can anybody find out the errors...help me to reach 100% code coverage

page:

<apex:page controller="Brandingcls" sidebar="true">
<apex:pageMessages />
<apex:form >
    <table width="100%" align="center">
        <tr>
           <td style="font-size:15px" align="center">
              <b>Branding Visit</b>
           </td>
        </tr>
    </table>
    <table width="100%" align="center">
         <tr>
            <td>
               <apex:pageBlock >
                   <apex:pageBlockSection >
                      <apex:inputField value="{!brorder.Order_Date__c}" required="True"/>
                      <apex:inputField value="{!brorder.Account__c}" required="True"/>
                   </apex:pageBlockSection>
               </apex:pageBlock>
            </td>
         </tr>
    </table>
<apex:pageBlock >
            <apex:pageBlockTable value="{!bimaster}" var="b">
                <apex:column headerValue="Branding Items" value="{!b.name}"/>
                <apex:column headerValue="Quantity">
                      <apex:inputField value="{!b.Quantity__c}"/>
                </apex:column>
                <apex:column headerValue="Remarks">
                    <apex:inputField value="{!b.Remarks__c}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons location="bottom">
                   <apex:commandButton value="Save" action="{!save}"/>&nbsp;&nbsp;
                   <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

controller:
public class Brandingcls
{
   public Branding_Visit__c brvisit{get;set;}
    public Branding_Order__c brorder{get;set;}
    public list<Branding_Item_Master__c> bimaster{get;set;}
    public list<Branding_Line_Item__c> brlineitem{get;set;}
    public Brandingcls()
    {
        bimaster=[select id,Name,Remarks__c,Quantity__c from Branding_Item_Master__c];
          brorder=new Branding_Order__c(Order_Date__c=system.Today());
        brvisit=new Branding_Visit__c(); 
    }
       
    public PageReference save()
    {
      list<Branding_Item_Master__c> lstbritem =new  list<Branding_Item_Master__c>();
          for(Branding_Item_Master__c bim :bimaster)
            {
                   if(bim.Quantity__c>0)
                      {
                        lstbritem.add(bim);
                      } 
            }
        if(lstbritem.Size()>0)
          {
        insert brorder;
        brvisit.Account__c = brorder.Account__c;
        brvisit.Visit_Date__c = brorder.Order_Date__c;
        insert brvisit;
        list<Branding_Line_Item__c> lstbli = new list<Branding_Line_Item__c>();     
          for(Branding_Item_Master__c bm:lstbritem)
             {
                  Branding_Line_Item__c brandli= new Branding_Line_Item__c();
                  
                   
                      brandli.Branding_Order__c=brorder.id;
                      brandli.Branding_Type__c=bm.id;
                      brandli.Quantity__c=bm.Quantity__c;
                      brandli.Remarks__c=bm.Remarks__c;
                      lstbli.add( brandli);
                
                  
             }
             if(lstbli.Size()>0)
             {
                   insert lstbli;
              }
          }
          else
          {
              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter atleast One Quantity to Create Branding Visit and Branding Order'));
              return Null;
          }
             pagereference ref=new pagereference( '/' +brvisit.id); 
                ref.setredirect(true);
                   return ref;
    }
   
   
    public PageReference cancel()
    {
       return null;
    }
}

testclass:
@istest
public class testBrandingcls{
   static testmethod  void  testsave(){
     Branding_Order__c br=new Branding_Order__c(Order_Date__c=system.today(),Account__c='testnaveen');
       insert br;
     Branding_Visit__c bv=new Branding_Visit__c(Visit_Date__c=system.today(),Account__c='testvisit');
       insert bv;
     Branding_Line_Item__c blm=new Branding_Line_Item__c(Branding_Type__c='Branding_Item_Master__c.id',Branding_Order__c='Branding_Order__c.id',Quantity__c=12);
        insert blm;
    Brandingcls obj= new Brandingcls();
          obj.save();
          obj.cancel();
    PageReference p=Page.Brandingvfpage;
        Test.setCurrentPage(p);      
   }
}

Thanks in advance...


PratikPratik (Salesforce Developers) 
Hi Naveen,

You can refer to the links which will help you write test classes for 100% coverage.

https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm

Thanks,
Pratik

Gaurav NirwalGaurav Nirwal
Try this
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;
}
}


Naveen KvpNaveen Kvp
I am posting this for test class not for controller gaurav...help me writing test class for above posted custom controller....
Mani RenusMani Renus
Which errors your getting in test class?