---
schema_version: "1"
id: e328aa4832383d4aa203242c9c84f8bbbe01852c676efdf5d07fde22bd65deac
title: Testing Your CanvasLifecycleHandler Implementation
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/canvas-framework/guides/platform-connect/using/lifecycle/canvas-testing-your-canvaslifecyclehandler.md
canonical_url: https://developer.salesforce.com/docs/platform/canvas-framework/guide/canvas-testing-your-canvaslifecyclehandler.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 1b5b973ffab81af0330de41e3d83ec2a4aec5152942942624c2ec10094f65894
products:
  - Canvas Developer Guide
  - Platform Developer Content
---

# Testing Your CanvasLifecycleHandler Implementation

You can use the Canvas.Test class to test your `Canvas.CanvasLifecycleHandler.onRender()` implementation without having to run your canvas app.

Use Canvas.Test to create a test Canvas.RenderContext with mock application and environment context data. Call `Canvas.Test.testCanvasLifecycle()` with the mock RenderContext and your CanvasLifecycleHandler implementation to verify that your CanvasLifecycleHandler is being invoked correctly.

Use `Canvas.Test.mockRenderContext()` to create a mock RenderContext. You can provide initial mock application and environment context data or let Canvas.Test use a default set of mock data. You provide initial mock application and environment context data in two Maps of key-value pairs. Use the predefined Canvas.Test key name constants as your keys. The following example sets the app name in the application context data and the sublocation in the environment context data.

<!-- otherprops: set_clipboard -->

```apex
Map<String,String> appValues = new Map<String,String>();
appValues.put(Canvas.Test.KEY_NAME,'AppName');

Map<String,String> envValues = new Map<String,String>();
envValues.put(Canvas.Test.KEY_SUB_LOCATION,'mobileDevice');

```

For the full list of context keys that Canvas.Test provides, see “Test Constants” in the [Apex Code Developer Guide](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_chapter.htm).

When you’ve got a mock RenderContext, you can call `Canvas.Test.testCanvasLifecycleHandler()` with the RenderContext and your CanvasLifecycleHandler. This call invokes the handler’s `onRender()` method, and passies the mock RenderContext as context data.

Here’s an example test class that uses Canvas.Test. The test class has three methods.

- `testDefaultMockValues()` invokes MyCanvasLifecycleHandler, using the default Canvas.Test mock context data.
- `testOverriddenAppValues()` invokes MyCanvasLifecycleHandler, using a custom mock RenderContext with mock app URL and version application context data.
- `testOverriddenEnvironmentValues()` invokes MyCanvasLifecycleHandler, using a custom mock RenderContext with mock display location, location URL, and custom parameter environment context data. The custom parameters are set through the Canvas.EnvironmentContext interface after the mock RenderContext is created.

<!-- otherprops: set_clipboard -->

```apex
-@isTest
global class MyCanvasLifecycleHandlerTest {

    static testMethod void testDefaultMockValues(){
        // Test handler using the default mock RenderContext Canvas.Test creates
        MyCanvasLifecycleHandler handler = new MyCanvasLifecycleHandler();
        Canvas.Test.testCanvasLifecycle(handler,null);
    }

    static testMethod void testOverriddenAppValues(){
        // Test handler with some mock application context values
        Map<String,String> appValues = new Map<String,String>();
        appValues.put(Canvas.Test.KEY_CANVAS_URL,'https://myserver.com:6000/myAppPath');
        appValues.put(Canvas.Test.KEY_VERSION,'3.0');

        Canvas.RenderContext mock = Canvas.Test.mockRenderContext(appValues,null);
        MyCanvasLifecycleHandler handler = new MyCanvasLifecycleHandler();
        Canvas.Test.testCanvasLifecycle(handler,mock);
    }

    static testMethod void testOverriddenEnvironmentValues(){
        // Test handler with some mock environment context values
        Map<String,String> envValues = new Map<String,String>();
        envValues.put(Canvas.Test.KEY_DISPLAY_LOCATION,'Chatter');
        envValues.put(Canvas.Test.KEY_LOCATION_URL,
            'https://myinstance.salesforce.com/_ui/core/chatter/ui/ChatterPage');
        MyCanvasLifecycleHandler handler = new MyCanvasLifecycleHandler();
        Canvas.RenderContext mock = Canvas.Test.mockRenderContext(null,envValues);
        // Directly update the mock RenderContext and set the custom parameters
        mock.getEnvironmentContext().setParametersAsJSON(
            '{\"one\":1,\"two\":2,\"bool\":true,\"stringVal\":\"some string\"}');
        Canvas.Test.testCanvasLifecycle(handler,mock);
    }

}
```
