I have a controller extension for a visual force page that currently has 0% coverage. A test was never written for it and I'm quite a novice when it comes to programming but I've got to start somewhere.

Here is the code for the page, controller, AND the code for the test as I've attempted to write it. I've been researching for a day and cannot figure out how to do this, could someone kindly direct me?

VF Page

<apex:page standardController="Case" extensions="CreateCaseController" showHeader="false"> 
<apex:includeScript value="/support/console/20.0/integration.js"/>
    sforce.console.setTabTitle('New Case');
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="New Case">
            <apex:pageBlockButtons >
                <apex:actionStatus id="status">
                    <apex:facet name="start">
                        <apex:outputpanel >
                    <apex:facet name="stop">
                        <apex:outputpanel >
                            <apex:commandButton value="Save" action="{!Save}" rerender="error1,error2" status="status"/>
                            <apex:commandButton value="Cancel" action="{!Cancel}" immediate="true" />
            <apex:outputLabel value="Account:  " for="acc"/ >
            <apex:outputField id="acc" value="{!case.accountid}" /><br/>
            <apex:outputLabel value="Contact:  " for="con"/>
            <apex:outputField id="con" value="{!case.contactid}" />

            <apex:pageBlockTable value="{!$ObjectType.Case.FieldSets.CaseQuickCreate}" var="f">
                <apex:outputField value="{!case.contactid}"/>
                <apex:outputField value="{!case.accountid}"/> 
                <apex:column value="{!f.Label}">
                    <apex:facet name="header"></apex:facet>
                <apex:column >
                    <apex:inputField value="{!Case[f]}" required="{!f.Required}" />

Controller extension -

public class CreateCaseController{

public Account account{get; set;} 
public Contact contact{get; set;}
public Case cs{get; set;}
public Boolean isSaveSuccess{get; set;}
Public string retURL{get; set;}
Public string csPhone{get; set;}

public CreateCaseController(ApexPages.StandardController controller){

    cs=((case) controller.getRecord());


public void createCase(){

    insert cs;
    isSaveSuccess = true;

Test class -

public class CreateCaseController_Test {

    public static testMethod void testCreateCase() {

        Account a = new Account(name='Test Account');
        insert a;
        Contact c = new Contact(lastname='Test Contact', accountid=a.id); 
        insert c; 
        Case cs = new Case(subject='Test Case'); 
        insert cs; 

        PageReference pageRef = Page.CreateCase; 
        pageRef.getParameters().put('aid', a.id); 

        ApexPages.StandardController stdCase = new ApexPages.StandardController(cs);
        CreateCaseController cc_ctrlr = new CreateCaseController(stdCase);


Test class edited to current state

  • if this is a controller that create a Case you are close - 1) no need to mock a Case in testmethod unless you are doing duplicate checking, (2) you pass an empty Case (new Case() to instantiate stdCase; and (3) - you need system asserts after cc_ctlr.createCase() to verify that the Case created matches expectations
    – cropredy
    Aug 26 '15 at 23:33

Your code is 0% covered because you have a case controller extension and you are not passing a case standard controller as a parameter.

Have a look on this post

  • Could you please help me understand why I need to pass a case to the page that is only used to create a case? Aug 26 '15 at 22:05
  • When using class extension you are trying to leverage standard controller .... To do so you need something that connect both controllers (the standard and extension) ... You pass the standard controller (not the object) to the extension contractor Aug 26 '15 at 22:23
  • This, "pass the standard controller (not the object)" may be the key to get me going in the right direction. Thank you! I'll be back to mark this as solved as soon as I verify where I'm headed. Aug 26 '15 at 22:41
  • Definite progress (66%) but since I'm passing a case to the controller my createCase method is failing. System.DmlException: Insert failed. First exception on row 0 with id 50018000002zePUAAY; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] What is the syntax to test the createCase method? cc_ctrlr.createCase(); doesn't work. Aug 26 '15 at 22:55
  • Your case is already inserted in the test class when u said "insert cs; " ... you need to remove this line then call the extension method ... otherwise you are trying to insert the existing case. Aug 26 '15 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.