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
Akshay MhetreAkshay Mhetre 

how to write a test class for which including Try Catch

Hello Everyone...I am not aware how to write a test class  which include Try Catch and having Aura unabled.... Please help. So I can learn too.

public with sharing class FISuperVisorRefireChecker {

    @AuraEnabled
    public static string refireCase(String recordId){
        try {
            if(String.isNotBlank(recordId)){
                List<Case> caseToUpdate = [select Id,FI_Stage__C,Decision__c,Agent_Case_Status__c,Fi_Agent__c,FI_Refire_Toggle__c
            from case where Id=: recordId];
            for(Case caseRecord: caseToUpdate){
                    if(String.isNotBlank(caseRecord.Decision__c) && !caseRecord.FI_Refire_Toggle__c){
                        return 'You cannot refire your decisioned case';
                    }
                    else if(caseRecord.Agent_Case_Status__c == 'Refire'){
                        return 'This case is already re-fired.'; 
                    }else if(caseRecord.FI_Agent__c == null){
                        return 'Please assign an agent before re-firing.'; 
                    }else if(caseRecord.FI_Stage__C == 'FI Verification In Progress'){
                        return 'You cannot refire as the case is still assigned to the agent.';
                    }
                    else{
                        caseRecord.Agent_Case_Status__c = 'Refire';
                    }
                } 
                update caseToUpdate;
                return 'Case was refired.Please ask agent to check';
            }else{
                return 'Something went wrong please try again';
            }
            
            
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
            //return JSON.serialize(e.getMessage());
        }
    }
}

Best Answer chosen by Akshay Mhetre
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the test class of your code.

@isTest
public class FISuperVisorRefireCheckerTest{

public static testMethod void refireCaseTest(){
    Account ac = new Account();
    ac.Name = 'Test Account';
    insert ac;

     Contact con=new Contact();
	 con.LastName='Data';
	 insert con;
	 
	 Case c = new Case();
    c.AccountId = ac.Id;
    c.ContactId = con.id;
    c.Origin = 'Phone';
    c.Status = 'New';
    c.Decision__c= '1';  
    c.FI_Refire_Toggle__c=true;
    c.Agent_Case_Status__c ='Refire';	
	c.FI_Stage__C='FI Verification In Progress';
	c.Agent_Case_Status__c ='Refire';
	
    insert c;

	
  Test.startTest();
  FISuperVisorRefireChecker.refireCase(c.id);
  Test.stopTest();
 
}
}

Do some need full changes according to your code.

Please mark it as the Best Answer if it helps you

Thank You

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the test class of your code.

@isTest
public class FISuperVisorRefireCheckerTest{

public static testMethod void refireCaseTest(){
    Account ac = new Account();
    ac.Name = 'Test Account';
    insert ac;

     Contact con=new Contact();
	 con.LastName='Data';
	 insert con;
	 
	 Case c = new Case();
    c.AccountId = ac.Id;
    c.ContactId = con.id;
    c.Origin = 'Phone';
    c.Status = 'New';
    c.Decision__c= '1';  
    c.FI_Refire_Toggle__c=true;
    c.Agent_Case_Status__c ='Refire';	
	c.FI_Stage__C='FI Verification In Progress';
	c.Agent_Case_Status__c ='Refire';
	
    insert c;

	
  Test.startTest();
  FISuperVisorRefireChecker.refireCase(c.id);
  Test.stopTest();
 
}
}

Do some need full changes according to your code.

Please mark it as the Best Answer if it helps you

Thank You

This was selected as the best answer
Akshay MhetreAkshay Mhetre

Hello Suraj and Charudatta..I am getting 80% code coverage..Need help ASAP. Please.

User-added image

For main class,return statements are not covering.(Bold Statements)  //Line no.10 ,17 ,28 ,29

>>>>> Main class ::

public with sharing class FISuperVisorRefireChecker {
    @AuraEnabled
    public static string refireCase(String recordId){
        try {
            if(String.isNotBlank(recordId)){
                List<Case> caseToUpdate = [select Id,FI_Stage__C,Decision__c,Agent_Case_Status__c,Fi_Agent__c,FI_Refire_Toggle__c
            from case where Id=: recordId];
            for(Case caseRecord: caseToUpdate){
                    if(String.isNotBlank(caseRecord.Decision__c) && !caseRecord.FI_Refire_Toggle__c){
                        return 'You cannot refire your decisioned case';             //Line no.10
                    }
                    else if(caseRecord.Agent_Case_Status__c == 'Refire'){
                        return 'This case is already re-fired.'; 
                    }else if(caseRecord.FI_Agent__c == null){
                        return 'Please assign an agent before re-firing.'; 
                    }else if(caseRecord.FI_Stage__C == 'FI Verification In Progress'){
                        return 'You cannot refire as the case is still assigned to the agent.';       // //Line no.17
                    }
                    else{
                        caseRecord.Agent_Case_Status__c = 'Refire';
                    }
                } 
                update caseToUpdate;
                return 'Case was refired.Please ask agent to check';
            }else{
                return 'Something went wrong please try again';
            }      
        } catch (Exception e) {                                                                           //Line No.28 29
            throw new AuraHandledException(e.getMessage());

            //return JSON.serialize(e.getMessage());
        }
    }
}

 

>>>>>My Test Class WITH 80% Code coverage ::

@isTest
public class FISuperVisorRefireCheckerTest {
@isTest
    public Static Void UnitTest(){
        
        PinCode__c picod = new PinCode__c();
        picod.PinCode__c = 123456;
        picod.Active__c = TRUE;
        insert picod;
        
        FI_Agent_Mapping__C fi= new FI_Agent_Mapping__c();
        fi.Name='Agent Name';
        fi.Agency_Name__c ='A R ASSOCIATES';
        fi.Fi_Agent__c=fi.Id;
        fi.PinCode__c = picod.Id;
        fi.Is_Auto_Allocation__c=false;
        insert fi;
        
        Case c = new Case();
        c.Status = 'New';
        c.Origin = 'Web';
        c.Decision__c= 'Negative (Not Recommended)';  
        c.FI_Refire_Toggle__c=true;
        c.FI_Agent__c =fi.Id;        
        Insert c;
        FISuperVisorRefireChecker.refireCase(c.Id);
    }
    @isTest
    public Static Void UnitTest1(){
        Case c = new Case();
        c.Status = 'New';
        c.Origin = 'Web';
        c.FI_Refire_Toggle__c = False;
        c.Agent_Case_Status__c = 'Refire';
        Insert c;
        FISuperVisorRefireChecker.refireCase(c.Id);
    }
    @isTest
    public Static Void UnitTest2(){
        Case c = new Case();
       c.Status = 'New';
        c.Origin = 'Web';
        Insert c;
        FISuperVisorRefireChecker.refireCase(c.Id);
    }
     @isTest
    public Static Void UnitTest3(){
        Case c = new Case();
        c.Status = 'New';
        c.Origin = 'Web';
        c.FI_Stage__C = 'FI Verification In Progress';
        Insert c;
        FISuperVisorRefireChecker.refireCase(c.Id);
    }
     @isTest
    public Static Void UnitTest4(){
        Case c = new Case();
        c.Status = 'New';
        c.Origin = 'Web';
        c.Agent_Case_Status__c = 'Refire';
        Insert c;
        
        FISuperVisorRefireChecker.refireCase(c.Id);
    }
     @isTest
    public Static Void UnitTest5(){
    
        FISuperVisorRefireChecker.refireCase(null);
    }
}

Suraj Tripathi 47Suraj Tripathi 47

I have seen your reply today

Do you still need coverage of your test class??

I think 80% is sufficient to deploy in the production.

why you remove Best Mark??

 

 

Akshay MhetreAkshay Mhetre

Hey Suraj...Sorry for that...Even I am wondering how that Best Mark changed into Like.  Your answers are always Best and helpful to me.

If possible can you cover those Red lines ??  If Yes,Please add corrections in my above code.

Thanks  :)