Testing Versioned Behavior in Apex Code

When you change the behavior in an Apex class or trigger for different package versions, it’s important to test that your code runs as expected in the different package versions. You can write test methods that change the package version context to a different package version by using the System.runAs method. You can only use System.runAs in a test method.

This sample shows a trigger with different behavior for different package versions. For more information about defining different behavior for package versions, see Version Apex Behavior.

The following test class uses the runAs method to verify the trigger’s behavior with and without a specific version: