I know this seems like a really stupid question. But I am still often amazed by the SalesForce platform. Consider the following test:
static testMethod void testLimitDmlStatements() { SObject record; // custom object set up elsewhere Test.startTest(); insert record; system.assertEquals(1, Limits.getDmlStatements()); Test.stopTest(); } I somehow get an assertion error:
System.AssertException: Assertion Failed: Expected: 1, Actual: 0
The documentation on the Limits Class states:
getDMLStatements()
Returns the number of DML statements (such as insert, update or the database.EmptyRecycleBin method) that have been called.
How is it possible that inserting a record would not count as a DML Statement? I have a similar update test and the same assertion fails. Strangely enough, if I add another operation, the count does increment.
static testMethod void testLimitDmlStatements() { SObject record; // custom object set up elsewhere Test.startTest(); insert record; SObjectFactory.create(User.sObjectType); // inserts a User system.assertEquals(1, Limits.getDmlStatements()); Test.stopTest(); } The above test passes, as does the below...
static testMethod void testLimitDmlStatements() { SObject record; // custom object set up elsewhere Test.startTest(); system.assertEquals(0, Limits.getDmlStatements()); insert record; system.assertEquals(1, Limits.getDmlStatements()); Test.stopTest(); }
Test.stopTest()do your results differ?DEBUGacross the board but it didn't work withFINESTeither.