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
Andy Morton 14Andy Morton 14 

Code coverage on public apex class displaying information

I'm new to apex coding and have run into a bit of a problem which I'm hoping someone can help with. 

Basically, I've been tasked with a Salesforce deployment for my company a part of which requires a customer portal for viewing cases. Following some early prototyping we decided to use a piece of apex programming to display the case list in a format that is suited to our needs. The list basically consists of a drop down box that has two options, depending on the option chosen the class will pull data from Salesforce. this is being used on a visualforce page that is embedded into the community site.

I've used this code in Sandox with no issues but when I try to deploy to Production it fails due to only 71% code coverage. 
 
public class CommunityCaseList {

public string options;
       
    public List<Case> getNewCases() {
    
        if(options=='Open')
        {
        
        List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
        }
    
        else if(options == 'Closed'){
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status = 'Closed' ORDER BY CreatedDate DESC];
            return results;            
        }
        
        else {
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
            
        }
    
    
    
    }
    
    public string getOptions() {
        return this.options;
    }
    public void setOptions(String Opt) {
        this.options=opt;
        
    }
    public list<SelectOption> getItems() {
      List<selectOption> options = new List<SelectOption>();
        options.add(new Selectoption('Open','Open'));
        options.add(new Selectoption('Closed','Closed'));
        return options;
        
    }
    public pagereference preview() {
        return null;
    }    

}



I've read up on it and believe I need to create a test class to run the code but when I'm trying to create one it displays the code as incorrect. This is the test class:
 
@isTest

private class CommunityCaseListTest {
 
    static testMethod void validateCommunityCaseList() {


public string options;
       
    public List<Case> getNewCases() {
    
        if(options=='Open')
        {
        
        List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
        }
    
        else if(options == 'Closed'){
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status = 'Closed' ORDER BY CreatedDate DESC];
            return results;            
        }
        
        else {
            
            List<Case> results = [SELECT id,CaseNumber,Status,Priority,Accountid,Account.Name,Contactid,contact.name,subject,CreatedDate,ClosedDate FROM Case WHERE Status != 'Closed' ORDER BY CreatedDate DESC];
            return results;
            
        }
    
    
    
    }
    
    public string getOptions() {
        return this.options;
    }
    public void setOptions(String Opt) {
        this.options=opt;
        
    }
    public list<SelectOption> getItems() {
      List<selectOption> options = new List<SelectOption>();
        options.add(new Selectoption('Open','Open'));
        options.add(new Selectoption('Closed','Closed'));
        return options;
        
    }
    public pagereference preview() {
        return null;
    }    

}

}



I'm not quite sure what I'm doing wrong or if there's an easier way to achieve the code coverage required. The tutorials I've read so far are for creating test classes that manipulate data whereas this is simply used to get data.

If anyone can help point me int he right direction it would be greatly appreciated. 

Many thanks

Andy
Best Answer chosen by Andy Morton 14
FARSANA PSFARSANA PS
Hai Andy,

Use this test class with 100% coverage..
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro
@isTest
public class CommunityCaseListTest {
    @testSetup static void dataCreation() {
        Account testAccount = new Account(name='Test Account');
        insert testAccount;
        contact testContact=new contact(Firstname='Unit',LastName='test',accountid=testAccount.id);
        insert testContact;
        Case testCase1 = new Case(Subject='Test Case1',Status='Closed',Contactid=testContact.id,Accountid=testAccount.id);
        insert testCase1;
        Case testCase2 = new Case(Subject='Test Case2',Status='Open',Contactid=testContact.id,Accountid=testAccount.id);
        insert testCase2;
        
    }
    @isTest
    public static void  CommunityCaseListMethodTest() 
    {
        CommunityCaseList CommunityCaseListobj=new CommunityCaseList();
        list<case> resultCase;
        CommunityCaseListobj.getItems();
        CommunityCaseListobj.preview();
        CommunityCaseListobj.setOptions('Open');
        resultCase=CommunityCaseListobj.getNewCases();
        system.assertNotEquals('Closed', resultCase[0].status);
        CommunityCaseListobj.setOptions('Closed');
        resultCase=CommunityCaseListobj.getNewCases();
        system.assertEquals('Closed', resultCase[0].status);
        string opt=CommunityCaseListobj.getOptions();
        system.assertEquals('Closed', opt);
        CommunityCaseListobj.setOptions('Other');
        resultCase=CommunityCaseListobj.getNewCases();
        system.assertNotEquals('Closed', resultCase[0].status);
    }
    
}
Hope this will help...

Thank you...

All Answers

FARSANA PSFARSANA PS
Hai Andy,

Use this test class with 100% coverage..
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro
@isTest
public class CommunityCaseListTest {
    @testSetup static void dataCreation() {
        Account testAccount = new Account(name='Test Account');
        insert testAccount;
        contact testContact=new contact(Firstname='Unit',LastName='test',accountid=testAccount.id);
        insert testContact;
        Case testCase1 = new Case(Subject='Test Case1',Status='Closed',Contactid=testContact.id,Accountid=testAccount.id);
        insert testCase1;
        Case testCase2 = new Case(Subject='Test Case2',Status='Open',Contactid=testContact.id,Accountid=testAccount.id);
        insert testCase2;
        
    }
    @isTest
    public static void  CommunityCaseListMethodTest() 
    {
        CommunityCaseList CommunityCaseListobj=new CommunityCaseList();
        list<case> resultCase;
        CommunityCaseListobj.getItems();
        CommunityCaseListobj.preview();
        CommunityCaseListobj.setOptions('Open');
        resultCase=CommunityCaseListobj.getNewCases();
        system.assertNotEquals('Closed', resultCase[0].status);
        CommunityCaseListobj.setOptions('Closed');
        resultCase=CommunityCaseListobj.getNewCases();
        system.assertEquals('Closed', resultCase[0].status);
        string opt=CommunityCaseListobj.getOptions();
        system.assertEquals('Closed', opt);
        CommunityCaseListobj.setOptions('Other');
        resultCase=CommunityCaseListobj.getNewCases();
        system.assertNotEquals('Closed', resultCase[0].status);
    }
    
}
Hope this will help...

Thank you...
This was selected as the best answer
Khan AnasKhan Anas (Salesforce Developers) 
Hi Andy,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
@isTest
public class TestCaseList {

    static testMethod void testmethod1() {        
        CommunityCaseList obj = new CommunityCaseList();
        obj.options='Open';
        obj.getNewCases();
        obj.getOptions();
        obj.setOptions('New');
        obj.getItems();
        obj.preview();
    }
    
    static testMethod void testmethod2() {
        CommunityCaseList obj = new CommunityCaseList();
        obj.options='Closed';
        obj.getNewCases();
    }
    
    static testMethod void testmethod3() {
        CommunityCaseList obj = new CommunityCaseList();
        obj.options='Working';
        obj.getNewCases();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Andy Morton 14Andy Morton 14
Thanks Khan and Farsana. I ended up using Khan's code and it's now deployed successfully. 
Andy Morton 14Andy Morton 14
Best answer changed as I've been informed by a Salesforce developer that testing codes that do not assert anything are considered bad tests and could cause issues further down the line. Farsana's code appears to assert and gives 100% code coverage as well.