0

I'm new to test classes, can anyone give me a help on this?

Here's the class:

global class CallTimerController
{
    @AuraEnabled
    global static Integer getMaxCallDuration(){
        Id pId = UserInfo.getProfileId();
        system.debug('Perfil do usuário: '+pId);
        system.debug('Nome do perfil do usuário: '+[select name from Profile where id = :pId]);

        try {
            //ONTAP__OnCallSettings__c setting = ONTAP__OnCallSettings__c.getInstance(pId);
            //return setting.ONTAP__Max_Call_Duration__c.intValue();
            return 50000;
        }
        catch (exception e) {
            return 99999999;
        }
    }

    @AuraEnabled
    global static void completeCall( Id callId ){

        CallProcessor callProcessor = new CallProcessor( callId );
        callProcessor.completeCall();

    }

}

I have the test for the firt return of 50000, but I don't know how to test the return of 99999999 inside the catch exception.

What I have so far:

@isTest
private class CallTimerControllerTest {

    @isTest static void testMaxCallDuration() {

        Test.startTest();
        Integer callDuration = CallTimerController.getMaxCallDuration();
        Test.stopTest();
        System.assertEquals(50000, callDuration);


    }
}
  • You have two lines of the try block commented out, are they normally part of this class, or should they be removed? As written, I would not expect return 50000; to throw any exceptions by itself. – MatthewGTS Apr 16 at 13:47
  • In addition to these answers, you should avoid using exceptions when an if-statement would suffice. They are on the order of 100 times slower than a simple if statement, which can cause performance issues if called repeatedly (e.g. in a loop). – sfdcfox Apr 16 at 14:35
2

To be able to test something that happens in a catch block, your test needs to cause an appropriate exception in the corresponding try block.

With those two lines of code commented out, causing an exception is impossible.

That said, I don't think that using exceptions is appropriate in this code you've given us. What you have right now is very close to an anti-pattern called "exceptions as flow control".

Getting rid of the try/catch entirely, and using some basic error checking in its place is likely a better solution, and will make it more clear what you need to do to gain the coverage you want.

Given

global static Integer getMaxCallDuration(){
    Id pId = UserInfo.getProfileId();
    system.debug('Perfil do usuário: '+pId);
    system.debug('Nome do perfil do usuário: '+[select name from Profile where id = :pId]);

    ONTAP__OnCallSettings__c setting = ONTAP__OnCallSettings__c.getInstance(pId);

    if(setting != null){
        return setting.ONTAP__Max_Call_Duration__c.intValue();
    }else{
        return 99999999;
    }
}

You can make one unit test method where you insert an ONTAP__OnCallSettings__c record to test the if block, and a separate unit test method where you do not insert a record for that custom setting to test the else block.

Just for sake of completeness, in unit tests, you need to create almost all of the data that you end up using in the test. Some things like User records, custom metadata type records, and a few other things (collectively known as "setup objects") are still available for you, but everything else (like records for custom settings) need to be explicitly created.

3

you need to have inside try block some code, that could throw an exception. For now, this code couldn't throw an exception. It depends on the logic, that you want to test. As an example, code, that could throw QueryException

global class CallTimerController{

    global static Account getAccount()
        try {
            return [
                select Id, Name
                from Account
                limit 1
            ];
        }catch (Exception ex) {
            return new Account(Name = 'Account from catch block');
        }
    }
}

and you can test it by checking returned value:

@isTest
static void testCatchException(){
    Test.startTest();
        Account actual = CallTimerController.getAccount();
    Test.stopTest();
    System.assertEquals('Account from catch block', actual.Name, 'Exception should be thrown');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.