Thursday, 24 December 2015

Apex Testing goodness in spring 16

Spring 16 release notes has been great read .If you have not read ,find some time and do a quick scan to discover some really cool features .

Apex Testing enhancement coming  in spring 16 caught my attention and this blog post covers them in detail .

1)Ability to set createdDate value for test data before Test.startTest()

The below test class demonstrates this new feature 

2)Test Suite

Test suites provide ability to run group of test classes together .On developer console you will find that you have ability to create a New Suite and use New Suite Run to run multiple suites or a single suite .Test Suites functionality is also available through Tooling API .I wont be surprised if this feature is added in some of IDE like Mavensmate or Welkins Suite 

The Suite Manager helps to add or remove Test classes to existing suites or form a new suite or delete existing test suites.

3)Stop a Test Run That’s Failing Miserably

You can now configure test runs to stop executing new tests after a given number of tests fail. You no longer need to waste your time waiting for the results of a run that you’re going to rerun after fixing issues in your org. You can set how many tests can fail in test runs that you execute both in the Developer Console and using the Tooling API.

The Developer Console’s test run configuration panes have a new Settings button. 
To specify how many tests can fail before a test run or test suite run is canceled, click Settings, enter a value for Number of failures allowed, and then click OK. This value applies for all test runs that you execute, until you close the Developer Console or set a new value.

I don't see this yet in spring 16 preview org and i assume it will be delivered .

4)Call Test.startTest() to Reliably Reset Limits in Apex Tests

5)Use @future to Avoid the Dreaded MIXED_DML_OPERATION Error in Apex Tests

To avoid mixed DML we used future method to separate transactions and this caused failures in test class .There are number of workarounds available 

1)We wrapped the code in future method with system.RunAs 
2)Also we  enclosed the code block that makes the future method call within Test.startTest and Test.stopTest statements.

check below link to know the workaround used before this release

All these workarounds no more needed and test class now does not throw mixed DML

Check the example documented for more details 

6)Test support for asynchronous WSDL based callouts

There is a neat example on how to approach test case for asynchronous WSDL callout in apex guide

Overall there has been great improvement in Testing framework of the apex .Test classes are very important for all apex code on platform .This needs to be seriously taken and great to see SFDC doing its best to enhance testing capabilities .

Introducing Lightning Base Components

Lightning Base Components are great addition to the platform and in fact revolutionary .One of the concerns around lightning component ...