You need to sign in to do that
Don't have an account?
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());
}
}
}
Hi,
Please find the test class of your code.
Do some need full changes according to your code.
Please mark it as the Best Answer if it helps you
Thank You
All Answers
Hi,
Please find the test class of your code.
Do some need full changes according to your code.
Please mark it as the Best Answer if it helps you
Thank You
Hello Suraj and Charudatta..I am getting 80% code coverage..Need help ASAP. Please.
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);
}
}
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??
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 :)