Sunday, 1 September 2013

Why we can’t write test class inside an apex class.

Starting from summer’13 sales force announced that the test class should be written outside of an apex class. You cannot save apex code with version 28.0 or later if you have written test method inside an apex class.

The reason behind the scene is:

While executing an apex class/ trigger sales force load the apex code into cache (a high speed memory) to make better performance. If you have written test class inside an apex class those test methods are also loaded into the cache memory and they stay in the cache till execution completes. There is no need to load these test code while executing apex class for any business logic.

The sales is a shared environment and if every apex class loads some test code into the cache memory there will be a lot of space underutilized in the system, thus causing others to wait. So sales force now won’t allow developers to write test class inside an apex class for apex code saved using API version 28 or later. But the existing code can be saved with earlier API version.


  1. Thanks for the information.

  2. Didn't Notice it has been depreciated. Thanks for the Info