+ Start a Discussion
sswaminasswamina 

APEX class executing sucessfully in ApexTestRunner but no records inserted in Org

Hi all,

 

I wrote a class to create accounts and test class to simulate the sample accounts. When I run with apex test runner the execution is sucessful and almost 98% code coverage however the records does not get inserted. Is that by design that when you run from debugger no records are created in org? Thanks much for your help.

 

Here is what I get in debug log:

17:47:31.390 (390135000)|STATEMENT_EXECUTE|[42]
17:47:31.390 (390153000)|STATEMENT_EXECUTE|[43]
17:47:31.390 (390197000)|HEAP_ALLOCATE|[43]|Bytes:4
17:47:31.390 (390297000)|VARIABLE_ASSIGNMENT|[42]|account|{"Name":"Penuvchev","Phone":"416-783-4433","Site":"Primary","BillingCity":"ABC","BillingPostalCode":"M5M","OwnerId":"005Q0000000hcC7IAI","email_address__c":"andrewpen@rogers.com","Fax":"416-276-8080","BillingStreet":"250 Lawrence Avenue  (15 more) ...","BillingState":"ON"}|0xf43b20
17:47:31.390 (390321000)|STATEMENT_EXECUTE|[42]
17:47:31.390 (390339000)|STATEMENT_EXECUTE|[43]
17:47:31.390 (390384000)|HEAP_ALLOCATE|[43]|Bytes:4
17:47:31.390 (390430000)|VARIABLE_ASSIGNMENT|[40]|accountOwner|"Tara Gibson"
17:47:31.390 (390449000)|STATEMENT_EXECUTE|[40]
17:47:31.390 (390465000)|STATEMENT_EXECUTE|[41]
17:47:31.390 (390481000)|VARIABLE_SCOPE_BEGIN|[41]|newAccounts|LIST|true|false
17:47:31.390 (390603000)|VARIABLE_ASSIGNMENT|[41]|newAccounts|[{"Name":"Dr. Jeremy Kurtz","Phone":"416-789-0705","Site":"Primary","BillingCity":"Toronto","BillingPostalCode":"M5M 12","OwnerId":"00540000000sPQbAAM","email_address__c":"@rogers (4 more) ...","Fax":"416-789-937","BillingStreet":"533 Avenue","BillingState":"ON"}]
17:47:31.390 (390626000)|STATEMENT_EXECUTE|[42]
17:47:31.390 (390719000)|VARIABLE_ASSIGNMENT|[42]|account|{"Name":"Dr. Jeremy Kurtz","Phone":"416-789-0705","Site":"Primary","BillingCity":"Toronto","BillingPostalCode":"M5M 12","OwnerId":"00540000000sPQbAAM","email_address__c":"@rogers (4 more) ...","Fax":"416-789-937","BillingStreet":" Avenue","BillingState":"ON"}|0x167f32d
17:47:31.390 (390742000)|STATEMENT_EXECUTE|[42]
17:47:31.390 (390759000)|STATEMENT_EXECUTE|[43]
17:47:31.390 (390801000)|HEAP_ALLOCATE|[43]|Bytes:4
17:47:31.390 (390833000)|STATEMENT_EXECUTE|[47]
17:47:31.390 (390880000)|STATEMENT_EXECUTE|[47]
17:47:31.390 (390898000)|STATEMENT_EXECUTE|[48]
17:47:31.390 (390914000)|VARIABLE_SCOPE_BEGIN|[48]|insertResults|LIST|true|false
17:47:31.390 (390964000)|DML_BEGIN|[48]|Op:Insert|Type:Account|Rows:3
17:47:31.391 (391044000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:47:31.507 (507221000)|DML_END|[48]
17:47:31.507 (507275000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:47:31.507 (507517000)|VARIABLE_ASSIGNMENT|[48]|insertResults|[{"success":true,"id":"001Q000000N8C66IAF"},{"success":true,"id":"001Q000000N8C67IAF"},{"success":true,"id":"001Q000000N8C68IAF"}]
17:47:31.507 (507549000)|STATEMENT_EXECUTE|[49]
17:47:31.507 (507624000)|STATEMENT_EXECUTE|[49]
17:47:31.507 (507643000)|STATEMENT_EXECUTE|[50]
17:47:31.507 (507683000)|USER_DEBUG|[50]|DEBUG|Records for Accounts inserted successfully
17:47:31.507 (507713000)|STATEMENT_EXECUTE|[51]
17:47:31.507 (507778000)|METHOD_EXIT|[56]|01pQ0000000DvDb|AccountHelper.createAccounts()
17:47:32.111 (507831000)|CUMULATIVE_LIMIT_USAGE
17:47:32.111|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 3 out of 10000
  Number of script statements: 81 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invo

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

To answer the original question completely, for clarity: DML statements will persist in all non-test cases, even in a System Log window or Execute Anonymous block (say, a custom third-party app). Test methods are the only time where all DML actions will be rolled back.

 

This means that any function that is labelled a test method, and is run from a Metadata API call or the Setup screen, and all of the code that it covers, regardless of where the code actually resides, is automatically rolled back at the end of the test method.

 

For example, if a test method calls a function in a class that calls modifies records and calls a trigger, which in turn calls a function that causes even more triggers to fire, and workflow rules to be evaluated, at the end of that test method, all of the changes that occurred never happened. Inserted records cease to exist, updated records show retain their original values, and deleted records are no longer deleted.

 

If it makes it easier to visualize this, each test method has implicit code, which looks like this:

 

 

public static testmethod void test() {
  // set up a save point.
  Database.SavePoint _sp = Database.setSavePoint();

  // actual test method code here

  // discard the transaction.
  Database.rollback(_sp);
}

 

Of course, their logic is a little more complicated (i.e. emails are also not sent, etc), but this is in essence what is going on.

 

Do not use ApexTestRunner to run Execute Anonymous blocks, because tests never save their data.

All Answers

Cory CowgillCory Cowgill

DML Statements that occur inside a test method never get committed to the Database.

 

This is by design so that your unit tests to no persiste data to your org.

sswaminasswamina

Thanks very very much, this helps a lot. The DML statements are not directly in test method but in the custom APEX account creation class.

 

Also I have custom object which I use to import the data in from excel, and have a trigger on 'after insert'  this custom object that calls my custom APEX account creation class. The trigger executes sucessfully, the custom object record gets inserted however the account does not get created. Any thoughts why this could be?

Shashikant SharmaShashikant Sharma

Hi,

 

Actulay when you run a testMethod no data gets comminted to database. In a testMethod you can create instance of a class or invoke a trigger but context remain for testExecutionso no data commited. You can verify it by using Static variable Test.IsRunningTest , this will always return true any where when you run test.

 

 

sfdcfoxsfdcfox

To answer the original question completely, for clarity: DML statements will persist in all non-test cases, even in a System Log window or Execute Anonymous block (say, a custom third-party app). Test methods are the only time where all DML actions will be rolled back.

 

This means that any function that is labelled a test method, and is run from a Metadata API call or the Setup screen, and all of the code that it covers, regardless of where the code actually resides, is automatically rolled back at the end of the test method.

 

For example, if a test method calls a function in a class that calls modifies records and calls a trigger, which in turn calls a function that causes even more triggers to fire, and workflow rules to be evaluated, at the end of that test method, all of the changes that occurred never happened. Inserted records cease to exist, updated records show retain their original values, and deleted records are no longer deleted.

 

If it makes it easier to visualize this, each test method has implicit code, which looks like this:

 

 

public static testmethod void test() {
  // set up a save point.
  Database.SavePoint _sp = Database.setSavePoint();

  // actual test method code here

  // discard the transaction.
  Database.rollback(_sp);
}

 

Of course, their logic is a little more complicated (i.e. emails are also not sent, etc), but this is in essence what is going on.

 

Do not use ApexTestRunner to run Execute Anonymous blocks, because tests never save their data.

This was selected as the best answer
sswaminasswamina

Thanks very much for explanation, it helps to understand the behaviour

 

Thanks all for the prompt replies