1

I am looking at fixing this test class issue where I get an error saying "Apex unit test classes should have at least one System.assert() or assertEquals() or AssertNotEquals() call" My Class is this

public with sharing class ActualCasesAPEXController {
    @AuraEnabled        
    public static Decimal getActualCases(){      
        
        AggregateResult[] groupedResults = [SELECT SUM(Case__c)actualcases FROM Case WHERE ((Status = 'Closed') and (ClosedDate = THIS_MONTH) and (OwnerId = :UserInfo.getUserId())) WITH SECURITY_ENFORCED];
        Decimal sum = (Decimal)groupedResults[0].get('actualcases');
        return sum;        
        
    }
}

and my test class looks like this with Assert changes I made, just wondering if this correct way of fixing it?

@isTest
public class ActualCasesAPEXControllerTest {
    static testMethod void ActualCasesAPEXControllerTest() {
        ActualCasesAPEXController.getActualCases();
        System.assert(ActualCasesAPEXController.getActualCases()!= null, 'Its not null');
    }
}

1 Answer 1

2

When testing your Apex classes, you should be testing actually functionality not just adding asserts in for no reason. I'd recommend looking at the Apex Testing Trailhead module to help explain some of the reasons for this.

At a minimum your test class should be expanded to:

  1. Have multiple scenarios. At the very least you should have a test method in which there are Cases for retrieval and one where there is not. This ensures that you're actually testing the various scenarios your code will encounter.

  2. Construct data in a TestSetup method to help create the data that is required for your scenarios.

  3. Asserts should actually test what is returned from your code versus what you are expecting to happen. In this case, you should know how many cases are assigned to the user that is calling the method in your test and use System.assertEquals to ensure that what you are expecting has occurred. This ensures that you are testing the logic of your class not just the fact that it can run.

A pseudocoded example of what your test method could look like if you take these into account looks something like this:

@IsTest
public class ActualCasesAPEXControllerTest{

    @TestSetup
    static void setupTestData(){
        // Create Users for each scenario (where they should be distinct)
        List<User> testUsers = new List<User>();

        // Create User who will own cases and User who will not own cases

        // Insert Test Users
        insert testUsers();

        List<Case> testCases = new List<Case>();
        // Create the cases you need to test with, 
        // ensuring you assign them to the correct test user
        
    }

    @isTest
    public static void testUserWithCases(){
        //Get the approrpiate test user

        System.runAs(testUser){

            //Call the method
            Decimal numberOfCases = 
              ActualCasesAPEXController.getActualCases();

            /* Now use assert to actually test the result. 
               You know how many cases you're expecting, so 
               you need to assert what was returned.*/
            System.assertEquals(expectedNumberOfCases, 
                                numberOfCases, 
              'The User should have x number of cases assigned.');
        }
    }

    @isTest
    public static void testUserWithoutCases(){
        //Get the approrpiate test user

        System.runAs(testUser){

            //Call the method
            Decimal numberOfCases = 
                     ActualCasesAPEXController.getActualCases();

            /* Now use assert to actually test the result. 
               You know how many cases you're expecting, 
               so you need to assert what was returned. */
            System.assertEquals(0, numberOfCases, 
                 'The User should have no of cases assigned.');
        }
    }

}

Note: Although you are asking about using System.assert/assertEquals/assertNotEquals in your question, Salesforce recommends to use the newer Assert Class.

2
  • Thanks @CallumMacErlich. Each code fix is presenting a huge learning curve. Commented Aug 2, 2023 at 2:26
  • No worries @SmitaHodiggeri, I completely know how that feels. Where possible Trailhead as well as the developer docs are your friend, and if there's a Developer Community Group that you can join in your area it's a great way to find a mentor. Commented Aug 2, 2023 at 2:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .