Apex unit test sees no data
Asked Answered
F

2

10

I have created a task in my org with subject "Email Task"

i have written apex code as

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;     
  }
}

I have created one test class to test apex code as

@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);

 }
}
} 

when I run this test I am getting 0 as ROW count. please refer apex.log file

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

Why I am getting 0 ROW COUNT ?

Foremost answered 21/3, 2012 at 7:1 Comment(0)
R
20

This is because as of Spring 2012, the data will not be in the org during testing and must be recreated from your test methods unless you use the IsTest(SeeAllData=true) annotation. From the Apex manual:

Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create.

You must create test data for each test. You can disable this restriction by annotating your test class or test method with the IsTest(SeeAllData=true) annotation. For more information, see IsTest(SeeAllData=true) Annotation.

Rok answered 21/3, 2012 at 7:46 Comment(0)
S
7

You need to insert a task with the subject "Email Task" at the start of your test method. As of the latest release, test methods can not access data already in the system which ensures that tests will be considerably more consistent with coverage between orgs. It also means we no longer have to add test specific clauses to code to ensure they select the right records when querying a large dataset!

Sigmund answered 21/3, 2012 at 7:46 Comment(1)
Definitely. This change is going to cause some to get frustrated but in the end it will be better for everyone. I like to create support classes that test methods call which generate test data. It's even better now that there won't be the potential for random actual data to foul up what we expect the results to be based on the test data we insert. If, for example, I insert an "Email Task" record, I'll know there will only be one in the test so count() will == 1, not potentially > 1.Rok

© 2022 - 2024 — McMap. All rights reserved.