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
Gina GarciaGina Garcia 

Test Class Coverage is only 25%, please help.

Hello guys, 
Below are parts of my apex class and test class.
This method is part of apex class that  is not covered.

Apex method class :

public List<Opportunity> getAccountOppList()
    {
        accountOppList = [SELECT id, Name, Donor_Name__c, Donor_Name_Id__c, CloseDate, Campaign_Fund__c, Amount, Type, Parent_Campaign__c, Gift_Method__c
            FROM Opportunity WHERE (ForecastCategoryName = 'Closed') AND (Account.id = :account.id OR Soft_Credit_Account__r.id = :account.id 
            OR Matched_Donor_Account__r.id = :account.id) ORDER BY Closedate DESC];
        
        return accountOppList;
        
    }

Part of Test class :
 List<Opportunity> accountOppList = New List<Opportunity>();
       accountOppList = [SELECT id, Name, Donor_Name__c, Donor_Name_Id__c, CloseDate, Campaign_Fund__c, Amount, Type, Parent_Campaign__c, Gift_Method__c
            FROM Opportunity WHERE (ForecastCategoryName = 'Closed') AND (Account.id = :accid OR Soft_Credit_Account__r.id = :accid 
            OR Matched_Donor_Account__r.id = :accid) ORDER BY Closedate DESC];
       System.AssertNotEquals(accountOppList,null);

Please let me know what do I need to add in the test class to increase the code coverage. 

Many thanks, 
Gina
Heather ThompsonHeather Thompson
Based on the test class that you provided, your coverage should be 0% since you aren't invocing the class at all so far as I can tell, you're just repeating the same logic that is in the original class. 

As a best practice, you should create your own data in your test classes and assert expected behavior. All the code you provided does is mimic the same logic as the original class - which doesn't test anything and ideally doesn't return anything unless you've already inserted data in your test class (or used seeAllData = true - which isn't recommended).

Ideally, you'd start off with something like this:
Database.SaveResult r = database.insert(new Account(name='test'));
database.insert(new Opportunity(AccountID = r.getID(), ForecastCategoryName = 'Closed'));
//set your class account variable
className.getAccountOppList();
//assert expexted behavior


I'd also suggest going through the Trailhead module on test classes: https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro

 
devedeve
Hi Gina,

Instead of calling query again in test class you have to set up or insert data for Opportunity fulfilling all conditions which you have applied in WHERE clause of query and then just call the function in testclass to increase your code coverage.

Sample Code for test class:

@isTest
private class testClass {

   @testsetup
    static void setUpdata() {
        Account account = new Account();
        insert account;
        Opportunity testOpportunity = new Opportunity();
        testOpportunity.Soft_Credit_Account__r.id = account.id;
        insert testOppurtunity;
    }
    static void testMethod() {
         List<Oppurtunity> oppList = ClassName.getAccountOppList();

        System.assert(true, oppList.Size()>0);
    }
}

 
Gina GarciaGina Garcia
Hi Heather, thank you for your reply. That's only part of the test class, the other part is I created the test data. Get Outlook for iOS