Salesforce Spring15 Test Class Additions

Sharing is Caring

Salesforce has finally introduced a way to create test data for an entire test class; this will be a huge time savings for developers.

The rollback of records created during execution happens at the end of the test class’ execution. By only inserting records once, we should see tests run faster and require less system resources.

Lately, I have been updating all of my test classes to use this new @testSetup annotation. One thing to keep in mind, is that it will not set static variables and has a bug for package developers.

Here’s some sample code:

private class join_Controller_Test {

	@TestSetup static void CreateTestData() {
		Account acc = Util_TestSetup.createAccount();

	static testMethod void Test_Load() {

		Account acc = [select Id from Account];

                PageReference p = new PageReference('apex/internal_join');
                p.getParameters().put('acc', acc.Id);


                internalJoin_Controller controller = new internalJoin_Controller();
		system.assertEquals(null, controller.OnLoad() );
		system.assertNotEquals(null, controller.acc );


I hope that the addition of @testSetup will finally result in better tests on Salesforce. As most programmers with test experience know, testing in Salesforce isn’t usually true unit testing.

Most are usually coded to be integration tests instead. Unit tests are tests that written to verify small sections (“units”) of code; integration testing is testing how the different units work together.

Another common, and disturbing trend, is that most programmers are simply coding unit tests to just get the 75% coverage and not using asserts. By using asserts, the tests can help significantly reduce bugs before code leaves development.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.