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
Abby StockerAbby Stocker 

Help with test class!

I have received some pretty awesome help from here correcting my code so I figured I would try to see if anyone can help me write a test class. My training (newbie) on test classes so far only include validating if statements, creating accounts with field values populated etc.. Help! Thank you!!! The 2 classes are for this purpose : Code 1 is looking for the id of the current case record. Code 2 is looking for the task id's associated with that current case record. 
Code 1
public class currentCaseRecord {
        Case currentRecord;
    public currentCaseRecord (ApexPages.StandardController controller){
        currentRecord = [Select Id FROM Case Where Id =:ApexPages.CurrentPage().getparameters().get('id')];
        }
    public case getcurrentRecord(){
        return currentRecord;
    }
}

Code 2
public class RelatedTasks {                
    List<Task> tasks = [SELECT id, subject from Task WHERE WhatId =:ApexPages.CurrentPage().getparameters().get('id')];                             
    }
Ajay K DubediAjay K Dubedi
Hi Stocker,

* First you need to make some change in you controller class and make it using best Practices to make classes

Controller class----->
public class currentCaseRecord
{
    Case currentRecord{get;set;}
    public currentCaseRecord (ApexPages.StandardController controller)
    {
        currentRecord = [Select Id FROM Case Where Id =:controller.getId()];
    }
    public case getcurrentRecord()
    {
        return currentRecord;
    }
}

Controller Class------>
public class RelatedTasks 
{
    public RelatedTasks(ApexPages.StandardController controller)
    {
        List<Task> tasks = [SELECT id, subject from Task WHERE WhatId =:controller.getId()];   
    }
}

Test Class------>
@isTest
public class Deve_Test 
{
    @isTest 
    public static void test_method()
    {
        case ca = new case();
        ca.Status = 'New';
        ca.Origin = 'Phone';
        task ta = new task();
        ta.Status = 'completed';
        ta.Subject = 'call';
        ta.Priority = 'Low';
        Test.startTest();
        Database.SaveResult str = Database.insert(ca,false);
        System.assertEquals(True, str.isSuccess());
        ApexPages.currentPage().getParameters().put('abc', String.valueOf(ca.Id));
        ApexPages.StandardController sc = new  ApexPages.StandardController(ca);
        currentCaseRecord st = new currentCaseRecord(sc);  
        st.getcurrentRecord();
        Database.SaveResult str1 = Database.insert(ta,false);
        System.assertEquals(True, str1.isSuccess());
        ApexPages.currentPage().getParameters().put('abc', String.valueOf(ta.Id));
        ApexPages.StandardController sc1 = new  ApexPages.StandardController(ta);
        RelatedTasks st1 = new RelatedTasks(sc1);  
        Test.stopTest();
    }
}

* Code coverage is 100% for both controller classes 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Abby StockerAbby Stocker
Hello! Thank you very very much for your assistance. Unfortunately thought the test class is not passing? I am reading through it now to see what I can do but if you have any clues I would appreciate it. Thank you!!!!