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
D.YounaiD.Younai 

Pleas tell me how to write Extension's test code.

Hi,Community!

 

I want to test the following code.

Plead tell me how to write test code.

 

public with sharing class LogACallControllerExtension {
    
    public Task task;
    
    ApexPages.Standardcontroller controller;
    
    public LogACallControllerExtension(ApexPages.StandardController controller) {
        this.task = (Task)controller.getRecord();
        User u = [Select Id From User Where id = :UserInfo.getUserId()];
        List<Lead> lead = [Select Id,Email,Phone From Lead Where Lead.Id =:ApexPages.currentPage().getParameters().get('who_id')];
        task.WhoId = lead[0].Id;
        task.WhatId = ApexPages.currentPage().getParameters().get('what_id');
        task.Status = ApexPages.currentPage().getParameters().get('tsk12');
        task.OwnerId = u.Id;
    }
    
   
    public PageReference save() {
        Task t = new Task();
        t.ownerId = task.ownerId;
        t.status = task.status;
        t.subject = task.subject;
        t.whoId = task.whoId;
        t.activityDate = task.activityDate;
        t.Description = task.Description;
        insert t;   
        return null;
    }

}

 Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
PrakashbPrakashb

Hi,

 

You can use the following class,

 

public static void testmethod  logcallextensiontest(){

 

      Task t = new Task();

//fill all the values needed for task.

      Insert t;

 

   ApexPages.Standardcontroller std = new ApexPages.standardcontroller(t);

  LogCallControllerExtension logcall = new Logcallextension(std);

//You can use System.assert methods here to validate the data before you save.

 logcall.save();

}

All Answers

PrakashbPrakashb

Hi,

 

You can use the following class,

 

public static void testmethod  logcallextensiontest(){

 

      Task t = new Task();

//fill all the values needed for task.

      Insert t;

 

   ApexPages.Standardcontroller std = new ApexPages.standardcontroller(t);

  LogCallControllerExtension logcall = new Logcallextension(std);

//You can use System.assert methods here to validate the data before you save.

 logcall.save();

}

This was selected as the best answer
D.YounaiD.Younai

Hi,Prakashb.

 

Thank you taught me.
I added the class.
But I get the following error.

 

"unexpected token: 'testmethod'"

 

What should I do?

Thanks.

PrakashbPrakashb

Hi

 

Sorry for the typo..

 

it is static testmethod void instead of public static void testmethod..

D.YounaiD.Younai

Hi,

 

Sorry to keep pestering you.

 

I get the following error.

"Invalid type: Logcallextension"

 

How do I do next?

Thanks.

PrakashbPrakashb

Again sorry for the typo..

 

It should be your class name in both the places..

 

LogCallControllerExtension logcall = new LogCallControllerExtension (std);

D.YounaiD.Younai

Sorry to keep pestering you.

First, it summarizes the current code.

static testmethod void LogCallControllerExtension(){
 
      Task t = new Task();
//fill all the values needed for task.
      Insert t;
 
   ApexPages.Standardcontroller std = new ApexPages.standardcontroller(t);
  LogCallControllerExtension logcall = new LogCallControllerExtension(std);
//You can use System.assert methods here to validate the data before you save.
 logcall.save();
}

 

 

I get the following error.

"Invalid type: LogCallControllerExtension"

 

How do I do next?

Thanks.

D.YounaiD.Younai

Hi,

 

I'm Sorry.
I was a typo.

 

static testmethod void LogACallControllerExtension(){
 
      Task t = new Task();
        ApexPages.currentPage().getParameters().put('who_id','00QU0000002mMkw');
        ApexPages.currentPage().getParameters().put('tsk12','Completed');
        User u = [Select Id From User Where id =:UserInfo.getUserId()];
        List<Lead> lead = [Select Id,Email,Phone From Lead Where Lead.Id =:ApexPages.currentPage().getParameters().get('who_id')];
        t.ownerId = u.Id;
        t.status = ApexPages.currentPage().getParameters().get('tsk12');
        t.subject = 'Call';
        t.whoId = ApexPages.currentPage().getParameters().get('who_id');
        t.activityDate = date.parse('2012/07/03');
        t.Description = 'test data';
//fill all the values needed for task.
      Insert t;
 
   ApexPages.Standardcontroller std = new ApexPages.standardcontroller(t);
  LogACallControllerExtension logcall = new LogACallControllerExtension(std);
//You can use System.assert methods here to validate the data before you save.
 logcall.save();
}

 

I could be save.
After running the test, I get the following error.

"System.ListException: List index out of bounds: 0"

Error in this part:"LogACallControllerExtension logcall = new LogACallControllerExtension(std);"

I'm thought,but I cannot get an answer.

Please tell me what to do.
Thanks.

D.YounaiD.Younai

Hi,All.

 

Please tell me test code.

I come here to get help.

 

@isTest
public with sharing class LogACallControllerExtension {
    
    public Task task;
    
    ApexPages.Standardcontroller controller;
    
    public LogACallControllerExtension(ApexPages.StandardController controller) {
        this.task = (Task)controller.getRecord();
        User u = [Select Id From User Where id = :UserInfo.getUserId()];
        List<Lead> lead = [Select Id,Email,Phone From Lead Where Lead.Id =:ApexPages.currentPage().getParameters().get('who_id')];
        task.WhoId = lead[0].Id;
        task.WhatId = ApexPages.currentPage().getParameters().get('what_id');
        task.Status = ApexPages.currentPage().getParameters().get('tsk12');
        task.OwnerId = u.Id;
    }
    
    
    public PageReference save() {
        Task t = new Task();
        t.ownerId = task.ownerId;
        t.status = task.status;
        t.subject = task.subject;
        t.whoId = task.whoId;
        t.activityDate = task.activityDate;
        t.Description = task.Description;
        insert t;   
        return null;
    }


static testmethod void LogACallControllerExtension(){
 
      Task t = new Task();
        ApexPages.currentPage().getParameters().put('who_id','00QU0000002mMkw');
        ApexPages.currentPage().getParameters().put('tsk12','Completed');
        t.ownerId = '005U0000000NadXIAS';
        t.status = ApexPages.currentPage().getParameters().get('tsk12');
        t.subject = 'Call';
        t.whoId = ApexPages.currentPage().getParameters().get('who_id');
        t.activityDate = date.parse('2012/07/03');
        t.Description = 'test data';
      Insert t;
 
   ApexPages.Standardcontroller std = new ApexPages.standardcontroller(t);
System.assertEquals('00QU0000002mMkw', t.whoId);
  LogACallControllerExtension logcall = new LogACallControllerExtension(std);
//You can use System.assert methods here to validate the data before you save.
 logcall.save();
}
}

 

But, after running the test, I get the following error.


"System.ListException: List index out of bounds: 0"

Error in this part:"LogACallControllerExtension logcall = new LogACallControllerExtension(std);"

 

The following lists the log.

 

25.0 APEX_CODE,FINE;APEX_PROFILING,FINE;DB,INFO;VALIDATION,INFO;WORKFLOW,FINEST
14:27:33.518 (1518394000)|EXECUTION_STARTED
14:27:33.518 (1518466000)|CODE_UNIT_STARTED|[EXTERNAL]|01pU0000000xw7y|LogACallControllerExtension.LogACallControllerExtension
14:27:33.519 (1519109000)|METHOD_ENTRY|[2]|01pU0000000xw7y|LogACallControllerExtension.LogACallControllerExtension()
14:27:33.520 (1520925000)|METHOD_EXIT|[2]|LogACallControllerExtension
14:27:33.525 (1525420000)|DML_BEGIN|[44]|Op:Insert|Type:Task|Rows:1
14:27:33.564 (1564423000)|DML_END|[44]
14:27:33.564 (1564851000)|CONSTRUCTOR_ENTRY|[48]|01pU0000000xw7y|<init>(ApexPages.StandardController)
14:27:33.567 (1567829000)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|select Id from User where id = :tmpVar1
14:27:33.572 (1572295000)|SOQL_EXECUTE_END|[10]|Rows:1
14:27:33.574 (1574936000)|SOQL_EXECUTE_BEGIN|[11]|Aggregations:0|select Id, Email, Phone from Lead where Lead.Id = :tmpVar1
14:27:33.576 (1576760000)|SOQL_EXECUTE_END|[11]|Rows:0
14:27:33.577 (1577055000)|CONSTRUCTOR_EXIT|[48]|01pU0000000xw7y|<init>(ApexPages.StandardController)
14:27:33.577 (1577233000)|FATAL_ERROR|System.ListException: List index out of bounds: 0

Class.LogACallControllerExtension.<init>: line 12, column 1
Class.LogACallControllerExtension.LogACallControllerExtension: line 48, column 1
14:27:33.577 (1577385000)|FATAL_ERROR|System.ListException: List index out of bounds: 0

Class.LogACallControllerExtension.<init>: line 12, column 1
Class.LogACallControllerExtension.LogACallControllerExtension: line 48, column 1
14:27:32.553 (1577425000)|CUMULATIVE_LIMIT_USAGE
14:27:32.553|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Number of script statements: 19 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

14:27:32.553|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
14:27:32.553|STATIC_VARIABLE_LIST|
  double:MIN_NORMAL:0
  double:POSITIVE_INFINITY:0
  long:serialVersionUID:0
  Boolean:TRUE:0
  double:MIN_VALUE:0
  int:SIZE:0
  int[]:sizeTable:0
  char[]:DigitOnes:0
  char[]:DigitTens:0
  String:_sfdcSuppressedCodeLocations:0
  double:NaN:0
  double:NEGATIVE_INFINITY:0
  int:MIN_VALUE:0
  int:SIZE:0
  double:MAX_VALUE:0
  String:_sfdcAdditionalCodeLocations:0
  long:serialVersionUID:0
  int:MAX_EXPONENT:0
  String:_sfdcAdditionalCodeLocations:0
  int:MIN_EXPONENT:0
  Boolean:FALSE:0
  int:MAX_VALUE:0
  char[]:digits:0
  long:serialVersionUID:0

14:27:32.553|CUMULATIVE_LIMIT_USAGE_END

14:27:33.577 (1577740000)|CODE_UNIT_FINISHED|LogACallControllerExtension.LogACallControllerExtension
14:27:33.577 (1577760000)|EXECUTION_FINISHED
14:27:33.025|CUMULATIVE_PROFILING_BEGIN
14:27:33.025|CUMULATIVE_PROFILING|No profiling information for SOQL operations
14:27:33.025|CUMULATIVE_PROFILING|No profiling information for SOSL operations
14:27:33.025|CUMULATIVE_PROFILING|DML operations|
Class.LogACallControllerExtension.LogACallControllerExtension: line 44, column 1: Insert: SOBJECT:Task: executed 1 time in 39 ms

14:27:33.025|CUMULATIVE_PROFILING|method invocations|
External entry point: static testMethod void LogACallControllerExtension(): executed 1 time in 59 ms
Class.LogACallControllerExtension.LogACallControllerExtension: line 48, column 1: public LogACallControllerExtension(ApexPages.StandardController): executed 1 time in 12 ms
Class.LogACallControllerExtension.<init>: line 10, column 1: global static String getUserId(): executed 2 times in 7 ms
Class.LogACallControllerExtension.<init>: line 11, column 1: global public static System.PageReference currentPage(): executed 4 times in 4 ms
Class.LogACallControllerExtension.LogACallControllerExtension: line 35, column 1: global public static System.PageReference currentPage(): executed 3 times in 0 ms
Class.LogACallControllerExtension.LogACallControllerExtension: line 38, column 1: global public static System.PageReference currentPage(): executed 3 times in 0 ms
Class.LogACallControllerExtension.LogACallControllerExtension: line 36, column 1: global public static System.PageReference currentPage(): executed 3 times in 0 ms
Class.LogACallControllerExtension.<init>: line 9, column 1: global SObject getRecord(): executed 1 time in 0 ms
Class.LogACallControllerExtension.LogACallControllerExtension: line 40, column 1: global public static System.PageReference currentPage(): executed 3 times in 0 ms
Class.LogACallControllerExtension.LogACallControllerExtension: line 41, column 1: global public static Date parse(String): executed 1 time in 0 ms
Class.LogACallControllerExtension.LogACallControllerExtension: line 47, column 1: global public static void assertEquals(ANY, ANY): executed 1 time in 0 ms

14:27:33.025|CUMULATIVE_PROFILING_END

 

I can not be resolved in my skills.

Please help me.

Thanks.

D.YounaiD.Younai

Hi,All.

 

Has been successfully tested with the following code.

 

@isTest
public with sharing class LogACallControllerExtension {
    
    public Task task;
    
    ApexPages.Standardcontroller controller;
    
    public LogACallControllerExtension(ApexPages.StandardController controller) {
        this.task = (Task)controller.getRecord();
        User u = [Select Id From User Where id = :UserInfo.getUserId()];
//        List<Lead> lead = [Select Id,Email,Phone From Lead Where Lead.Id =:ApexPages.currentPage().getParameters().get('who_id')];
//        task.WhoId = lead[0].Id;
        task.WhoId = ApexPages.currentPage().getParameters().get('who_id');
        task.WhatId = ApexPages.currentPage().getParameters().get('what_id');
        task.Status = ApexPages.currentPage().getParameters().get('tsk12');
        task.OwnerId = u.Id;
    }
    
    
    public PageReference save() {
        Task t = new Task();
        t.ownerId = task.ownerId;
        t.status = task.status;
        t.subject = task.subject;
        t.whoId = task.whoId;
        t.activityDate = task.activityDate;
        t.Description = task.Description;
        insert t;   
        return null;
    }


static testmethod void LogACallControllerExtension(){
 
      Task task = new Task();
        task.ownerId = '005U0000000NadXIAS';
        task.status = 'Completed';
        task.subject = 'Call';
        task.whoId = '00QU0000002mMkw';
        task.activityDate = date.parse('2012/07/18');
        task.Description = 'test data';
      Insert task;

   PageReference pageRef = Page.instask;
   Test.setCurrentPage(pageRef);
        pageRef.getParameters().put('who_id','00QU0000002mMkw');
        pageRef.getParameters().put('tsk12','Completed');

   ApexPages.Standardcontroller std = new ApexPages.standardcontroller(task);
  LogACallControllerExtension lcall = new LogACallControllerExtension(std);
  Task TaskL = [select Id,description,ownerId,status,subject,activitydate from Task where activitydate = :date.parse('2012/07/18')];
  system.assertequals(TaskL.ownerId,'005U0000000NadXIAS');
  system.assertequals(TaskL.description,'test data');
  system.assertequals(TaskL.activitydate,date.parse('2012/07/18'));
  system.assertequals(TaskL.status,'Completed');
  system.assertequals(TaskL.subject,'Call');
//You can use System.assert methods here to validate the data before you save.
 lcall.save();
 
}

}

 

Thank you very much for everything.