Apex单元测试没有数据

时间:2012-03-21 07:01:43

标签: salesforce apex-code

我在我的组织中创建了一个主题为“电子邮件任务”的任务

我写了顶点代码为

public class  TaskInfoDAOClass
{
   public static List<Task> queryTaskInfo ()
    {
integer count = [SELECT count() FROM Task];
System.debug('Row Count :'+ count);
 List<Task> tasks= [SELECT Task.Id,Task.AccountId,Task.Status,Task.Account.Name FROM Task where Task.Subject='Email Task'];
  return tasks;     
  }
}

我创建了一个测试类来测试顶点代码

@isTest 
public class TestTaskInfoDAO
{
 public static testMethod void testQueryTaskInfo()
 {
List<Task> tasks = TaskInfoDAOClass.queryTaskInfo();
 System.debug ('this is a debug statement');

for (Task t : tasks)
{
 System.debug ('Status '+ t.Status);
  System.debug ('Account name '+ t.Account.Name);

 }
}
} 

当我运行此测试时,我得到0作为ROW计数。请参阅apex.log文件

24.0 APEX_CODE,FINE;APEX_PROFILING,FINE;DB,INFO;VALIDATION,INFO;WORKFLOW,FINEST
23:48:30.100 (100678000)|EXECUTION_STARTED
23:48:30.100 (100722000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000000k5Qw|TestTaskInfoDAO.testQueryTaskInfo
23:48:30.101 (101272000)|METHOD_ENTRY|[2]|01p90000000k5Qw|TestTaskInfoDAO.TestTaskInfoDAO()
23:48:30.101 (101414000)|METHOD_EXIT|[2]|TestTaskInfoDAO
23:48:30.101 (101494000)|METHOD_ENTRY|[1]|01p90000000k5Qr|TaskInfoDAOClass.TaskInfoDAOClass()
23:48:30.101 (101513000)|METHOD_EXIT|[1]|TaskInfoDAOClass
23:48:30.101 (101782000)|METHOD_ENTRY|[6]|01p90000000k5Qr|TaskInfoDAOClass.queryTaskInfo()
23:48:30.102 (102176000)|SOQL_EXECUTE_BEGIN|[5]|Aggregations:0|select count() from Task
23:48:30.106 (106893000)|SOQL_EXECUTE_END|[5]|Rows:0
23:48:30.106 (106965000)|SYSTEM_METHOD_ENTRY|[6]|String.valueOf(Object)
23:48:30.107 (107012000)|SYSTEM_METHOD_EXIT|[6]|String.valueOf(Object)
23:48:30.107 (107032000)|SYSTEM_METHOD_ENTRY|[6]|System.debug(ANY)
23:48:30.107 (107040000)|USER_DEBUG|[6]|DEBUG|Row Count :0
23:48:30.107 (107047000)|SYSTEM_METHOD_EXIT|[6]|System.debug(ANY)
23:48:30.107 (107385000)|SOQL_EXECUTE_BEGIN|[9]|Aggregations:0|select Task.Id, Task.AccountId, Task.Status, Task.Account.Name from Task where Task.Subject = 'Email Task'
23:48:30.109 (109847000)|SOQL_EXECUTE_END|[9]|Rows:0
23:48:30.109 (109930000)|METHOD_EXIT|[6]|01p90000000k5Qr|TaskInfoDAOClass.queryTaskInfo()
23:48:30.110 (110074000)|USER_DEBUG|[7]|DEBUG|this is a debug statement
23:48:30.789 (111361000)|CUMULATIVE_LIMIT_USAGE
23:48:30.789|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 7 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

23:48:30.789|TOTAL_EMAIL_RECIPIENTS_QUEUED|0
23:48:30.789|STATIC_VARIABLE_LIST|
  double:MAX_VALUE:0
  long:serialVersionUID:0
    int:MAX_EXPONENT:0
    int:MIN_EXPONENT:0
    Boolean:TRUE:0
    long:serialVersionUID:0
    double:POSITIVE_INFINITY:0
    double:MIN_NORMAL:0
    double:MIN_VALUE:0
    Boolean:FALSE:0
    int:SIZE:0
    char[]:DigitOnes:0
    int[]:sizeTable:0
    char[]:DigitTens:0
    double:NaN:0
    int:MAX_VALUE:0
    char[]:digits:0
    long:serialVersionUID:0
    double:NEGATIVE_INFINITY:0
    int:MIN_VALUE:0
    int:SIZE:0

23:48:30.789|CUMULATIVE_LIMIT_USAGE_END

23:48:30.111 (111444000)|CODE_UNIT_FINISHED|TestTaskInfoDAO.testQueryTaskInfo
23:48:30.111 (111452000)|EXECUTION_FINISHED
23:48:30.999|CUMULATIVE_PROFILING_BEGIN
23:48:30.999|CUMULATIVE_PROFILING|SOQL operations|
  Class.TaskInfoDAOClass.queryTaskInfo: line 5, column 1: [SELECT count() FROM Task]: executed 1 time in 5 ms
  Class.TaskInfoDAOClass.queryTaskInfo: line 9, column 1: [SELECT Task.Id,Task.AccountId,Task.Status,Task.Account.Name FROM Task where Task.Subject='Email Task']: executed 1 time in 3 ms

23:48:30.999|CUMULATIVE_PROFILING|No profiling information for SOSL operations
23:48:30.999|CUMULATIVE_PROFILING|No profiling information for DML operations
23:48:30.999|CUMULATIVE_PROFILING|method invocations|
  External entry point: public static testMethod void testQueryTaskInfo(): executed 1 time in 11 ms
  Class.TestTaskInfoDAO.testQueryTaskInfo: line 6, column 1: public static LIST<Task> queryTaskInfo(): executed 1 time in 8 ms
  Class.TestTaskInfoDAO.testQueryTaskInfo: line 9, column 1: global object iterator(): executed 2 times in 1 ms
  Class.TaskInfoDAOClass.queryTaskInfo: line 6, column 1: global public static String valueOf(Object): executed 2 times in 0 ms
  Class.TestTaskInfoDAO.testQueryTaskInfo: line 7, column 1: global public static void debug(ANY): executed 1 time in 0 ms

23:48:30.999|CUMULATIVE_PROFILING_END

为什么我得到0 ROW COUNT?

2 个答案:

答案 0 :(得分:20)

这是因为从2012年春季开始,测试期间数据不会出现在组织中,必须从测试方法中重新创建,除非您使用IsTest(SeeAllData = true)注释。来自Apex手册:

  

从使用Salesforce API 24.0及更高版本保存的Apex代码开始   之后,测试方法默认情况下无权访问预先存在的数据   在组织中,例如标准对象,自定义对象和   自定义设置数据,只能访问他们创建的数据。

     

您必须为每个测试创建测试数据。你可以禁用它   通过注释您的测试类或测试方法来限制   IsTest(SeeAllData = true)注释。有关更多信息,请参阅   IsTest(SeeAllData = true)注释。

答案 1 :(得分:7)

您需要在测试方法的开头插入一个主题为“电子邮件任务”的任务。从最新版本开始,测试方法无法访问系统中已有的数据,这可确保测试与orgs之间的覆盖范围更加一致。这也意味着我们不再需要在代码中添加测试特定子句,以确保在查询大型数据集时选择正确的记录!