function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Clement Debrueres-BartoliClement Debrueres-Bartoli 

Test Class for VF page showing Current Cases of the Account

Hi Salesforce friends,

I am still a beginner with Apex class and I have a requirement to create a VF page showing all the open cases of the Acount.

My VF page is
<apex:page standardController="Case" extensions="ShowCustomersOpenCases">
<apex:form > 
<apex:pageBlock >
<apex:pageBlockTable value="{!CA}" var="C" columnsWidth="1%,1%,1%" >                                                     
                <apex:column headerValue="Case Number" >
                <apex:commandLink value="{!C.CaseNumber}" action="/{!C.Id}" target="_blank" />
                </apex:column>                
                <apex:column headerValue="Status" value="{!C.Status}" />
                <apex:column headerValue="Created Date" value="{!C.CreatedDate}" />       
</apex:pageBlockTable>               
</apex:pageBlock>
</apex:form>
</apex:page>
My Apex Controller is
 
public with sharing class ShowCustomersOpenCases {

    public List<Case> CA { get; private set; }

    public ShowCustomersOpenCases(ApexPages.StandardController controller) {
        controller.addFields(new List<String>{ 'AccountId' });
        Case currentCase = (Case)controller.getRecord();
        CA = [Select Id, CaseNumber, CreatedDate, Status, Subject, ContactId FROM Case Where Status <> 'Closed' AND AccountId = :currentCase.AccountId and Id != :currentCase.Id Order by CreatedDate DESC Limit 100];
      }
}

But I dont manage to create a proper Test Class, after a lot of attempts.

Any good soul who could help me with that test Class?

Thank you very much
​​​​​​​
 
Raj VakatiRaj Vakati
Use this code and add all the required filed when you are inserting the account and contact om test class
 
@istest
public class ShowCustomersOpenCasesTest{
    Private Static testmethod void testCase(){    

	 

    //create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    insert acc;
  
    
    //create case
    Case c = new Case();
    //enter details
    c.AccountId = acc.Id;
    c.Type = 'My Type';
    c.Origin = 'My Origin';
    c.Status = 'My Status';
    insert c;
		
		
		//create case
    Case c1 = new Case();
    //enter details
    c1.AccountId = acc.Id;
    c1.Type = 'My Type';
    c1.Origin = 'My Origin';
    c1.Status = 'My Status';
    insert c1;
		
		ApexPages.currentPage().getParameters().put('id',c.id);
		ApexPages.StandardController stdLead = new ApexPages.StandardController(c);
	    ShowCustomersOpenCases  objMyLeadsController  = new ShowCustomersOpenCases (stdLead);
		
    }
}

 
Clement Debrueres-BartoliClement Debrueres-Bartoli
Hello Raj,

first of all thank you for your answer.

Unfortunately, I slightly modified your code to adapt to my environment, but it fails when I run the test, although I havent modified the elements of your code
 
@istest
public class ShowCustomersOpenCasesTest{
    Private Static testmethod void testCase(){    

    //create account
    Account acc = new Account();
    //enter details  
    acc.Name = 'Test Account';
    insert acc;
  
    
    //create case
    Case c = new Case();
    //enter details
    c.AccountId = acc.Id;
    c.subject = 'Help';
    c.Status = 'New';
    insert c;
        
        
        //create case
    Case c1 = new Case();
    //enter details
    c1.AccountId = acc.Id;
    c1.subject = 'Problem';
    c1.Status = 'New';
    insert c1;
        
        ApexPages.currentPage().getParameters().put('id',c.id);
        ApexPages.StandardController stdLead = new ApexPages.StandardController(c);
        ShowCustomersOpenCases  objMyLeadsController  = new ShowCustomersOpenCases (stdLead);
        
    }
}
Error message is: System.DMLException: Insert failed. First Exception on row 0; first error: Class.ShowCustomersOpenCasesTest.testCase: line 18, column 1.

Any idea on how this fails?

Thank you very much,

Clement
Raj VakatiRaj Vakati
Looks like case record is not inserting .. can you check is there any required  fields and validation rules ?? please setup case data to insert the data meet all validation etc .. 
Clement Debrueres-BartoliClement Debrueres-Bartoli
Hey Raj,
I double checked everything and made sure the fields that I create are correct, like this:
 
@istest
public class ShowCustomersOpenCasesTest{
    Private Static testmethod void testCase(){    

    //create account
    Account acc = new Account();
    //enter details  
    acc.Username__c= 'TestAccount';
    acc.Source_UID__c= '005';
    
    //create case
    Case c = new Case();
    //enter details
    c.Status = 'New';
    c.Subject = 'Hello'; 
    c.Source_UID__c = '005';
    
    insert c;
        
        //create case
    Case c1 = new Case();
    //enter details
    c1.Status = 'Closed';
    c1.Subject = 'Problem';  
    c1.Category__c = 'Registration';
    c1.SubCategory__c = 'Unable to register';
    c1.SubCategory_2__c = 'Unable to open an Account (other reason)';
    c1.Source_UID__c = '005';
    
    insert c1;
        
        ApexPages.currentPage().getParameters().put('id',c.id);
        ApexPages.StandardController stdLead = new ApexPages.StandardController(c);
        ShowCustomersOpenCases  objMyLeadsController  = new ShowCustomersOpenCases (stdLead);
        
    }
}

However the code coverage is only 40% (2/5) and the Test doesnt fully pass to show green check.

Any idea on how I can fix this maybe?

Thank you for your help

Clement