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
adrisseladrissel 

Help with @isTest class!

I have a class that uses the StandardSetController and I honestly have no clue what to do to get some decent code coverage on it.  I have written some basic parts of the test class, but can't figure out the rest.  Can someone help me understand what needs to be done with these kinds of test classes.

Here is the controller:
public with sharing class OutOfOffice {

    ApexPages.StandardSetController setCon;
    
    List<string> caseIds = new List<string>();
    
    List<Case> caseList = new List<Case>();
    
    public Case c;

    public Case getNewCase() {
    
        if(c == null){
            
            c = new Case();
        }
        
        return c;
    }
    
    public OutOfOffice (Apexpages.StandardSetController controller) {
    
        setCon = controller;
    
        caseIds = ApexPages.currentPage().getParameters().get('recs').split(',',-2);
    }
    
    public List<Case> getSelected() {
    
        caseList = [SELECT Subject,CaseNumber,Status FROM Case WHERE Id IN: caseIds];
    
        return caseList;
    }
    
    public Pagereference updateCases(){
    
        List<Case> updatedCases = new List<Case>();
        
        for(Case cse : caseList){
        
            cse.Out_Of_Office_Start__c = c.Out_Of_Office_Start__c;
            
            cse.Out_Of_Office_End__c = c.Out_Of_Office_End__c;
            
            cse.Out_Of_Office__c = c.Out_Of_Office__c;
            
            updatedCases.add(cse);
        }
        
        update updatedCases;

        return new Pagereference('/'+Case.getSObjectType().getDescribe().getKeyPrefix()+'?fcf='+ApexPages.currentPage().getParameters().get('retUrl'));
    }
    
    public Pagereference cancelUpdate(){

        return new Pagereference('/'+Case.getSObjectType().getDescribe().getKeyPrefix()+'?fcf='+ApexPages.currentPage().getParameters().get('retUrl'));
    }
}


Here is the current test class:
@isTest
private class OutOfOfficeTest{

    private static testmethod void testOutOfOffice() {
    
        // CREATE AND INSERT TEST CONTACT
        
        Contact con = new Contact (LastName = 'Smith', FirstName = 'Joe', Email = 'joesmith@test.com');
        
        insert con;
        
        
        // CREATE START AND END DATES
        
        Date startDate = date.today();
        
        Date endDate = startDate.addDays(1);
    
        
        // CREATE AND INSERT NEW CASE
        
        Case c = new Case(
        
            Out_Of_Office__c = true,
            Out_Of_Office_Start__c = startDate,
            Out_Of_Office_End__c = endDate,
            ContactId = con.Id
        );
        
        insert c;
    
        
        // CREATE AND INSERT NEW EMAIL
        
        EmailMessage em = new EmailMessage(
            FromAddress = 'test@example.com',
            Incoming = true,
            ToAddress = 'test@gmail.com',
            Subject = 'Test email',
            TextBody = 'Hello',
            ParentId = c.Id
        );
        
        insert em;
        
    
        // CREATE AND INSERT TEST CASE COMMENT
        
        CaseComment com = new CaseComment (
        
            CommentBody = 'test',
            ParentId = c.Id,
            IsPublished = true
        );    
        
        insert com;   
    }
}

I don't know where to go from here.

Please help!

Thanks ahead of time!!


nitesh gadkarinitesh gadkari
Hi adrissel,

You have not created instance of controller.It should be something like

create instance of your object;
ApexPages.StandardSetController controller=new ApexPages.StandardSetController(pass instance  of your  object);  
 
  GenerateInvoicePageController gipController=new GenerateInvoicePageController(controller);

  system.assertNotEquals(gipController, null);

use system.assertEquals(expected result,actual result); to check data you inserted

These are the links to help you further.

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm

Regards
Nitesh 

nitesh gadkarinitesh gadkari
Hi,
@isTest
private class OutOfOfficeTest{
    private static testmethod void testOutOfOffice() {
        // CREATE AND INSERT TEST CONTACT
        Contact con = new Contact (LastName = 'Smith', FirstName = 'Joe', Email = 'joesmith@test.com');
        insert con;
        system.assertEquals(con.lastname, 'smith');
  ApexPages.StandardSetController controller=new ApexPages.StandardSetController(con);  
   OutOfOffice OController=new OutOfOffice(controller);
  system.assertNotEquals(OController, null);
    
        //test page reference
       Pagereference page1=page.GenerateInvoicePage;
       test.setCurrentPage(page1);
        
        // CREATE START AND END DATES
        Date startDate = date.today();
        system.assertEquals(startdate, date.newInstance(2014,9,15));
        Date endDate = startDate.addDays(1);
        system.assertEquals(startdate, date.newInstance(2014,9,16));
        // CREATE AND INSERT NEW CASE
        Case c = new Case(
        
            Out_Of_Office__c = true,
            Out_Of_Office_Start__c = startDate,
            Out_Of_Office_End__c = endDate,
            ContactId = con.Id
        );
        insert c;
        system.assertEquals(c.Out_Of_Office__c, true);
        // CREATE AND INSERT NEW EMAIL
        EmailMessage em = new EmailMessage(
            FromAddress = 'test@example.com',
            Incoming = true,
            ToAddress = 'test@gmail.com',
            Subject = 'Test email',
            TextBody = 'Hello',
            ParentId = c.Id
        );
        
        insert em;
        system.assertEquals(em.FromAddress, 'test@example.com');
        // CREATE AND INSERT TEST CASE COMMENT
        
        CaseComment com = new CaseComment (
        
            CommentBody = 'test',
            ParentId = c.Id,
            IsPublished = true
        );    
        
        insert com;   
        system.assertEquals(com.commentbody,'test');
    }
}

use system.assertEquals(expected result,actual result); to check data you inserted

These are the links to help you further.

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm

Regards
Nitesh
adrisseladrissel
Hey Nitesh,

I get the following error:

Compile Error: Constructor not defined: [ApexPages.StandardSetController].<Constructor>(SOBJECT:Contact) at line 12 column 54

It doesn't like the "con" variable being passed in the controller, apparently.
Susheel Reddy BSusheel Reddy B
Hi Adrissel,

Did you get any solution for this test class. I have similar requiremnet and unbale to write test class. Please share the code if you have.
Thanks in advance.

Thanks,
Susheel Reddy