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
Parth SrivastavaParth Srivastava 

help with test class of standard controller of case object: please find below code of main class

public class ShowRelatedDataClass {

    // Properties
    private ID CaseId ;
    public List<String> items {get;set;}
    static Map<String,Map<Id,List<sObject>>> allvalues;
    public List<HealthCloudGA__EhrCondition__c> EHRConditions {get;set;}
    public List<HealthCloudGA__EhrObservation__c> EHRObservations {get;set;}
    public List<HealthCloudGA__EhrMedicationPrescription__c> EHRMedicationPrescriptions {get;set;}
    public List<HealthCloudGA__EhrMedicationStatement__c> EHRMedicationStatements {get;set;}
    
    // Constructor
    public ShowRelatedDataClass(ApexPages.StandardController controller) {
        caseId = ((Case)controller.getRecord()).Id;        
        case CaseRecord = [Select AccountId From Case Where Id = : caseId];
        
        EHRConditions = new List<HealthCloudGA__EhrCondition__c>();
        EHRConditions  = [Select Name From HealthCloudGA__EhrCondition__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
        
        
        EHRObservations = new List<HealthCloudGA__EhrObservation__c>();
        EHRObservations = [Select Name From HealthCloudGA__EhrObservation__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
        
        EHRMedicationPrescriptions  = new List<HealthCloudGA__EhrMedicationPrescription__c>();
        EHRMedicationPrescriptions  = [Select Name From HealthCloudGA__EhrMedicationPrescription__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];

        EHRMedicationStatements = new List<HealthCloudGA__EhrMedicationStatement__c>();
        EHRMedicationStatements = [Select Name From HealthCloudGA__EhrMedicationStatement__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];


    }
    
    // Method to create map of Data of different tables
    static  Map<Id,List<sObject>> parentmap(String object_name){
        String FieldSet = '';
        if(object_name == 'HealthCloudGA__CarePlanProblem__c')
            FieldSet = ' id,Name , HealthCloudGA__CarePlan__c ';
        if(object_name == 'HealthCloudGA__CarePlanGoal__c')
            FieldSet = ' Id, Name, HealthCloudGA__CarePlanProblem__c ';
        if(object_name  == 'Task')
            FieldSet = ' Id, Subject, HealthCloudGA__CarePlanGoal__c ';
        
        List<sObject> c2 =  Database.query('select '+FieldSet +' from ' + object_name );    
        Map<Id,List<sObject>> m2 = new  Map<Id,List<sObject>>();
        String parentid = '';
        for (sObject o2:c2) {
            parentid = null;
            if (object_name == 'HealthCloudGA__CarePlanProblem__c') {
                parentid = ((HealthCloudGA__CarePlanProblem__c) o2).HealthCloudGA__CarePlan__c;
            } 
            if (object_name == 'HealthCloudGA__CarePlanGoal__c') {
                parentid = ((HealthCloudGA__CarePlanGoal__c) o2).HealthCloudGA__CarePlanProblem__c;
            } 
            if (object_name == 'Task') {
                parentid = ((Task) o2).HealthCloudGA__CarePlanGoal__c ;
            }             
            if (parentid == null) continue;
            if (m2.containsKey(parentid)) {
                ((List<sObject>) m2.get(parentid)).add(o2);
            } else {
                m2.put(parentid, new List<sObject>{o2});
            }
        }
        return m2;
    }
    
    // Method to build pageblock Tables
    public  Component.Apex.PageBlockTable getHierarchy() {
        
        Map<Id,Case> c1 = new  Map<Id,Case>([select id, CaseNumber from Case Where Id = : caseId ]);      
        allvalues = new  Map<String,Map<Id,List<sObject>>>(); 
        allvalues.put('HealthCloudGA__CarePlanProblem__c',parentmap('HealthCloudGA__CarePlanProblem__c'));
        allvalues.put('HealthCloudGA__CarePlanGoal__c',parentmap('HealthCloudGA__CarePlanGoal__c'));
        allvalues.put('Task',parentmap('Task'));
        
        items = new List<String>();
        Component.Apex.PageBlockTable myTable = new  Component.Apex.PageBlockTable(var='item');
        myTable.expressions.value = '{!items}';
        
        Component.Apex.column clm = new Component.Apex.column(headerValue= 'Name'); 
        myTable.childComponents.add( clm );
        Component.Apex.OutputText outText = new Component.Apex.OutputText( );           
        outText.expressions.value = '{!item}';   
        outText.escape = false;   
        clm.childComponents.add( outText );     
        
        for (Id id1 :c1.keyset()) {
            Case o1 = (Case) c1.get(id1);
            items.add(o1.CaseNumber);
            getNodes('HealthCloudGA__CarePlanProblem__c',o1.id);
        }            
        return myTable;
    }
    
    // Method to add child data. 
    public void getNodes(String object_name,String parentid) {        
        String name = '';
        String id = '';        
        Map<Id,List<sObject>> map1 = (Map<Id,List<sObject>>) allvalues.get(object_name);      
        if (object_name == 'HealthCloudGA__CarePlanProblem__c') {
            if (map1.containsKey(parentid)) {
                List<HealthCloudGA__CarePlanProblem__c> lcob = (List<HealthCloudGA__CarePlanProblem__c>) map1.get(parentid);
                for (HealthCloudGA__CarePlanProblem__c cob:lcob) {
                    name = cob.name;
                    items.add('&nbsp;&nbsp;' + name);
                    getNodes('HealthCloudGA__CarePlanGoal__c',cob.id);
                    
                }
            }     
        } else if (object_name == 'HealthCloudGA__CarePlanGoal__c') {
                if (map1.containsKey(parentid)) {
                    List<HealthCloudGA__CarePlanGoal__c> lcob = (List<HealthCloudGA__CarePlanGoal__c>) map1.get(parentid);
                    for (HealthCloudGA__CarePlanGoal__c cob:lcob) {
                        name = cob.name;
                        items.add('&nbsp;&nbsp;&nbsp;&nbsp;' + name);
                        getNodes('Task',cob.id);
                    }
                }   
            } else if (object_name == 'Task') {
                if (map1.containsKey(parentid)) {
                    List<Task> lcob = (List<Task>) map1.get(parentid);
                    for (Task cob:lcob) {
                        name = cob.Subject;
                        items.add('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+name);
    }} } }}
Best Answer chosen by Parth Srivastava
Raj VakatiRaj Vakati
try this
@isTest
public class ShowRelatedDataClassRwsr
{
     private static testmethod void ShowRelatedDataClassTest()
     {


         Account obj = new Account(Name ='Test');
		 // add all reuiqed fields 
         insert obj;

		 
		 case newcase = new case();
// add all reuiqed fields 
newcase.Status = 'Open';
newcase.Origin = 'Phone';
newcase.AccountId = obj.Id ; 
insert newcase;

HealthCloudGA__EhrCondition__c  ehr = new HealthCloudGA__EhrCondition__c () ; 
ehr.Name ='Test';
// add all reuiqed fields 
ehr.HealthCloudGA__Account__c  = obj.Id ; 
insert ehr ; 


HealthCloudGA__EhrObservation__c   ehro = new HealthCloudGA__EhrObservation__c  () ; 
ehro.Name ='Test';
// add all reuiqed fields 
ehro.HealthCloudGA__Account__c  = obj.Id ; 
insert ehro ; 

HealthCloudGA__EhrObservation__c   ehrop = new HealthCloudGA__EhrObservation__c  () ; 
ehrop.Name ='Test';
// add all reuiqed fields 
ehrop.HealthCloudGA__Account__c  = obj.Id ; 
insert ehrop ; 


HealthCloudGA__EhrMedicationStatement__c    ehropm = new HealthCloudGA__EhrMedicationStatement__c   () ; 
ehropm.Name ='Test';
// add all reuiqed fields 
ehropm.HealthCloudGA__Account__c  = obj.Id ; 
insert ehropm ; 


         PageReference pageRef = Page.YOUR_PAGE;
         Test.setCurrentPage(pageRef);
         ApexPages.StandardController sc = new ApexPages.StandardController(newcase);
         ShowRelatedDataClass  relData = new ShowRelatedDataClass (sc);
		 relData.getHierarchy();

    }
}

 

All Answers

Raj VakatiRaj Vakati
try this
@isTest
public class ShowRelatedDataClassRwsr
{
     private static testmethod void ShowRelatedDataClassTest()
     {


         Account obj = new Account(Name ='Test');
		 // add all reuiqed fields 
         insert obj;

		 
		 case newcase = new case();
// add all reuiqed fields 
newcase.Status = 'Open';
newcase.Origin = 'Phone';
newcase.AccountId = obj.Id ; 
insert newcase;

HealthCloudGA__EhrCondition__c  ehr = new HealthCloudGA__EhrCondition__c () ; 
ehr.Name ='Test';
// add all reuiqed fields 
ehr.HealthCloudGA__Account__c  = obj.Id ; 
insert ehr ; 


HealthCloudGA__EhrObservation__c   ehro = new HealthCloudGA__EhrObservation__c  () ; 
ehro.Name ='Test';
// add all reuiqed fields 
ehro.HealthCloudGA__Account__c  = obj.Id ; 
insert ehro ; 

HealthCloudGA__EhrObservation__c   ehrop = new HealthCloudGA__EhrObservation__c  () ; 
ehrop.Name ='Test';
// add all reuiqed fields 
ehrop.HealthCloudGA__Account__c  = obj.Id ; 
insert ehrop ; 


HealthCloudGA__EhrMedicationStatement__c    ehropm = new HealthCloudGA__EhrMedicationStatement__c   () ; 
ehropm.Name ='Test';
// add all reuiqed fields 
ehropm.HealthCloudGA__Account__c  = obj.Id ; 
insert ehropm ; 


         PageReference pageRef = Page.YOUR_PAGE;
         Test.setCurrentPage(pageRef);
         ApexPages.StandardController sc = new ApexPages.StandardController(newcase);
         ShowRelatedDataClass  relData = new ShowRelatedDataClass (sc);
		 relData.getHierarchy();

    }
}

 
This was selected as the best answer
Parth SrivastavaParth Srivastava
Thanks for your reply  Raj Vakati.It Really helped, But still coverage is around 60%.
Can you suggest a few changes