---
schema_version: "1"
id: 0277f97a835f7f82487745e7abbb5f96432fc273256d73c9af3cb4592bc21442
title: Testing Versioned Behavior in Apex Code
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_manpkgs_behavior_testing.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_manpkgs_behavior_testing.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: edfead1a4b972fb081d5bc4ba338bf2eeb284034ba3f74b9aa52498fdfeb04bc
description: 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.
---
# 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](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_manpkgs_behavior.htm "Package developers can use conditional logic in Apex classes and triggers to exhibit different behavior for different versions. With this conditional logic, you can support existing behavior in classes and triggers in previous package versions while evolving the code.").

trigger oppValidation on Opportunity (before insert, before update) {

    for (Opportunity opp : Trigger.new){
    
        // Add a new validation to the package
        // Applies to versions of the managed package greater than 1.0
        if (System.requestVersion().compareTo(new Version(1,0)) > 0) {
            if (opp.Probability >= 50 && opp.Description == null) {
                opp.addError('All deals over 50% require a description');
            }
        }

        // Validation applies to all versions of the managed package.
        if (opp.IsWon == true && opp.LeadSource == null) {
            opp.addError('A lead source must be provided for all Closed Won deals');
        }
    }
}

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

@IsTest
private with sharing class OppTriggerTests{

    @IsTest
    static void testOppValidation(){

        // Set up 50% opportunity with no description
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Job';
        opp.Probability = 50;
        opp.StageName = 'Prospect';
        opp.CloseDate = System.today();

        // Test running as latest package version
        try{
            insert as user opp;
        }
        catch(DMLException e){
            Assert.isTrue(
                e.getMessage().contains(
                    'All deals over 50% require a description'),
                e.getMessage());
        }

        // Run test as managed package version 1.0
        System.runAs(new Version(1,0)){
            try{
                insert as user opp;
            }
            catch(DMLException e){
                Assert.isFalse(false, e.getMessage());
            }
        }

        // Set up a closed won opportunity with no lead source
        opp = new Opportunity();
        opp.Name = 'Test Job';
        opp.Probability = 50;
        opp.StageName = 'Prospect';
        opp.CloseDate = System.today();
        opp.StageName = 'Closed Won';

        // Test running as latest package version
        try{
            insert as user opp;
        }
        catch(DMLException e){
            Assert.isTrue(
                e.getMessage().contains(
                    'A lead source must be provided for all Closed Won deals'),
                e.getMessage());
        }

        // Run test as managed package version 1.0
        System.runAs(new Version(1,0)){
            try{
                insert as user opp;
            }
            catch(DMLException e){
                Assert.isTrue(
                    e.getMessage().contains(
                        'A lead source must be provided for all Closed Won deals'),
                    e.getMessage());
            }
        }
    }
}