You need to sign in to do that
Don't have an account?
Rohith Kumar 98
How to cover code of catch block in Test Class?
How can I also cover the catch block?
TriggerHandlerClass with highlighted code(catch block) that is not covered by Test Class:
Test Class:
TriggerHandlerClass with highlighted code(catch block) that is not covered by Test Class:
public class PaymentOrderTriggerHandler { public static void updatePaymentContactInformation(List<Payment_Order__c> paymentOrderList){ for(Payment_Order__c paymentOrder : paymentOrderList){ if(paymentOrder.Account__c != Null){ //Getting particular account id ID accountId = paymentOrder.Account__c; //Updating value to Payment Order from Account with above account id as reference try{ paymentOrder.Payee_Name__c = [SELECT Name FROM Account WHERE Id = :accountId].Name; paymentOrder.Phone_No__c = [SELECT Phone FROM Account WHERE Id = :accountId].Phone; paymentOrder.Address_Street_1__c = [SELECT BillingStreet FROM Account WHERE Id = :accountId].BillingStreet; paymentOrder.Address_Street_2__c = [SELECT ShippingStreet FROM Account WHERE Id = :accountId].ShippingStreet; paymentOrder.Zip__c = [SELECT ShippingPostalCode FROM Account WHERE Id = :accountId].ShippingPostalCode; paymentOrder.State__c = [SELECT ShippingState FROM Account WHERE Id = :accountId].ShippingState; paymentOrder.Country__c = [SELECT ShippingCountry FROM Account WHERE Id = :accountId].ShippingCountry; } catch(Exception error){ error.getMessage(); } } } } public static void insertAccountContactInformation(List<Payment_Order__c> paymentOrderList){ List<Account> accountList = new List<Account>(); for(Payment_Order__c paymentOrder : paymentOrderList){ if(paymentOrder.Account__c == null){ //Creating new record in Account with field values from Payment Order Account newAccount = new Account(Name = paymentOrder.Payee_Name__c, Phone = paymentOrder.Phone_No__c, BillingStreet = paymentOrder.Address_Street_1__c, ShippingStreet = paymentOrder.Address_Street_2__c, ShippingPostalCode = paymentOrder.Zip__c, ShippingState = paymentOrder.State__c, ShippingCountry = paymentOrder.Country__c); accountList.add(newAccount); } } //Inserting created account record try{ insert accountList; }catch(Exception error){ error.getMessage(); } //Tagging created account with corresponding Payment Order for(Account account : accountList){ for(Payment_Order__c paymentOrder : paymentOrderList){ if(paymentOrder.Payee_Name__c == account.Name && paymentOrder.Phone_No__c == account.Phone && paymentOrder.Address_Street_1__c == account.BillingStreet && paymentOrder.Address_Street_2__c == account.ShippingStreet && paymentOrder.Zip__c == account.ShippingPostalCode && paymentOrder.State__c == account.ShippingState && paymentOrder.Country__c == account.ShippingCountry) { paymentOrder.Account__c = account.Id; } } } } public static void searchAndCreateAccount(List<Payment_Order__c> paymentOrderList){ boolean found = false; List<Account> accountList = new List<Account>(); for(Payment_Order__c paymentOrder : paymentOrderList){ for(Account account : [SELECT Name, Phone, BillingStreet, ShippingStreet, ShippingPostalCode, ShippingState, ShippingCountry FROM Account]){ if(paymentOrder.Payee_Name__c == account.Name && paymentOrder.Phone_No__c == account.Phone && paymentOrder.Address_Street_1__c == account.BillingStreet && paymentOrder.Address_Street_2__c == account.ShippingStreet && paymentOrder.Zip__c == account.ShippingPostalCode && paymentOrder.State__c == account.ShippingState && paymentOrder.Country__c == account.ShippingCountry) { found = true; paymentOrder.Account__c = account.id; } } if(!found){ //Need to reuse - code available above Account newAccount = new Account(Name = paymentOrder.Payee_Name__c, Phone = paymentOrder.Phone_No__c, BillingStreet = paymentOrder.Address_Street_1__c, ShippingStreet = paymentOrder.Address_Street_2__c, ShippingPostalCode = paymentOrder.Zip__c, ShippingState = paymentOrder.State__c, ShippingCountry = paymentOrder.Country__c); accountList.add(newAccount); } } //Inserting created account record try{ insert accountList; }catch(Exception error){ error.getMessage(); } //Tagging created account with corresponding Payment Order for(Account account : accountList){ for(Payment_Order__c paymentOrder : paymentOrderList){ if(paymentOrder.Payee_Name__c == account.Name && paymentOrder.Phone_No__c == account.Phone && paymentOrder.Address_Street_1__c == account.BillingStreet && paymentOrder.Address_Street_2__c == account.ShippingStreet && paymentOrder.Zip__c == account.ShippingPostalCode && paymentOrder.State__c == account.ShippingState && paymentOrder.Country__c == account.ShippingCountry) { paymentOrder.Account__c = account.Id; } } } } public static void updateStatusAndOwner(List<Payment_Order__c> paymentOrderList){ for(Payment_Order__c paymentOrder : paymentOrderList){ if(paymentOrder.Payment_Amount__c > 2000){ paymentOrder.Status__c = 'Pending Approval'; ID queueId = [SELECT Queue.Id FROM queuesobject WHERE queue.name='Payment Order Pending Approval'].Queue.Id; paymentOrder.OwnerId = queueId; } } } }
Test Class:
@isTest public class PaymentOrderTriggerHandlerTest { @isTest public static void testUpdatePaymentContactInformationCheck(){ //Creating account Account account = new Account(Name = 'Test Account', Phone = '1234567890', BillingStreet = 'Test Billing Street', ShippingStreet = 'Test Shipping Street', ShippingPostalCode = '625014', ShippingState = 'Test State', ShippingCountry = 'Test country'); insert account; //Creating Payment Order with above account as lookup Payment_Order__c paymentOrder = new Payment_Order__c(Account__c = account.Id); insert paymentOrder; Test.startTest(); //Getting all test data String paymentOrderName = [SELECT Payee_Name__c FROM Payment_Order__c WHERE Account__c = :account.Id].Payee_Name__c; String paymentOrderPhone = [SELECT Phone_No__c FROM Payment_Order__c WHERE Account__c = :account.Id].Phone_No__c; String paymentOrderStreet1 = [SELECT Address_Street_1__c FROM Payment_Order__c WHERE Account__c = :account.Id].Address_Street_1__c; String paymentOrderStreet2 = [SELECT Address_Street_2__c FROM Payment_Order__c WHERE Account__c = :account.Id].Address_Street_2__c; String paymentOrderZip = [SELECT Zip__c FROM Payment_Order__c WHERE Account__c = :account.Id].Zip__c; String paymentOrderState = [SELECT State__c FROM Payment_Order__c WHERE Account__c = :account.Id].State__c; String paymentOrderCountry = [SELECT Country__c FROM Payment_Order__c WHERE Account__c = :account.Id].Country__c; System.assertEquals(account.Name, paymentOrderName); System.assertEquals(account.Phone, paymentOrderPhone); System.assertEquals(account.BillingStreet, paymentOrderStreet1); System.assertEquals(account.ShippingStreet, paymentOrderStreet2); System.assertEquals(account.ShippingPostalCode, paymentOrderZip); System.assertEquals(account.ShippingState, paymentOrderState); System.assertEquals(account.ShippingCountry, paymentOrderCountry); Test.stopTest(); } @isTest public static void testInsertAccountContactInformationCheck(){ //Creating Payment Order without lookup account Payment_Order__c paymentOrder = new Payment_Order__c(Payee_Name__c = 'Test Payment Order', Phone_No__c = '9876543210', Address_Street_1__c = 'Test Street 1', Address_Street_2__c = 'Test Street 2', Zip__c = '625014', State__c = 'Test State', Country__c = 'Test Country'); insert paymentOrder; //Getting Id, all data of account created by trigger ID accountID = [SELECT Account__c FROM Payment_Order__c WHERE Id = :paymentOrder.Id].Account__c; String accountName = [SELECT Name FROM Account WHERE Id = :accountId].Name; String accountPhone = [SELECT Phone FROM Account WHERE Id = :accountId].Phone; String accountBillingStreet = [SELECT BillingStreet FROM Account WHERE Id = :accountId].BillingStreet; String accountShippingStreet = [SELECT ShippingStreet FROM Account WHERE Id = :accountId].ShippingStreet; String accountZip = [SELECT ShippingPostalCode FROM Account WHERE Id = :accountId].ShippingPostalCode; String accountState = [SELECT ShippingState FROM Account WHERE Id = :accountId].ShippingState; String accountCountry = [SELECT ShippingCountry FROM Account WHERE Id = :accountId].ShippingCountry; Test.startTest(); System.assertEquals(paymentOrder.Payee_Name__c, accountName); System.assertEquals(paymentOrder.Phone_No__c, accountPhone); System.assertEquals(paymentOrder.Address_Street_1__c, accountBillingStreet); System.assertEquals(paymentOrder.Address_Street_2__c, accountShippingStreet); System.assertEquals(paymentOrder.Zip__c, accountZip); System.assertEquals(paymentOrder.State__c, accountState); System.assertEquals(paymentOrder.Country__c, accountCountry); Test.stopTest(); } @isTest public static void testSearchAndCreateAccountCheck(){ //Creating Account Account account = new Account(Name = 'Test Account', Phone = '1234567890', BillingStreet = 'Test Billing Street', ShippingStreet = 'Test Shipping Street', ShippingPostalCode = '625014', ShippingState = 'Test State', ShippingCountry = 'Test country'); insert account; //creating Payment Order Payment_Order__c paymentOrder = new Payment_Order__c(Payee_Name__c = 'Test Payment Order', Phone_No__c = '9876543210', Address_Street_1__c = 'Test Street 1', Address_Street_2__c = 'Test Street 2', Zip__c = '625014', State__c = 'Test State', Country__c = 'Test Country'); insert paymentOrder; //updating payment order to match above account data paymentOrder.Payee_Name__c = 'Test Account'; paymentOrder.Phone_No__c = '1234567890'; paymentOrder.Address_Street_1__c = 'Test Billing Street'; paymentOrder.Address_Street_2__c = 'Test Shipping Street'; paymentOrder.Zip__c = '625014'; paymentOrder.State__c = 'Test State'; paymentOrder.Country__c = 'Test Country'; update paymentOrder; //getting id of account associated with payment order ID paymentOrderAccountId = [SELECT Account__c FROM Payment_Order__c WHERE Id = :paymentOrder.Id].Account__c; Test.startTest(); System.assertEquals(account.Id, paymentOrderAccountId); //updating payment order to not match above account data paymentOrder.Phone_No__c = '7777777777'; update paymentOrder; //getting updated account lookup id paymentOrderAccountId = [SELECT Account__c FROM Payment_Order__c WHERE Id = :paymentOrder.Id].Account__c; System.assertNotEquals(account.Id, paymentOrderAccountId); Test.stopTest(); } @isTest public static void testUpdateStatusAndOwnerCheck(){ //Creating Payment Order with payment amount greater than 2000 Payment_Order__c paymentOrder = new Payment_Order__c(Payment_Amount__c = 2001); insert paymentOrder; //Getting status of payment order String status = [SELECT Status__c FROM Payment_Order__c WHERE Id = :paymentOrder.Id].Status__c; //getting Queue Id ID queueId = [SELECT Queue.Id FROM queuesobject WHERE queue.name='Payment Order Pending Approval'].Queue.Id; ID paymentOrderOwnerId = [Select OwnerId FROM Payment_Order__c WHERE Id = :paymentOrder.Id].OwnerId; Test.startTest(); System.assertEquals('Pending Approval', status); System.assertEquals(queueId, paymentOrderOwnerId); Test.stopTest(); } }
Here is a test method to cover the catch of the first method, do the same for the other methods: Please mark as the best answer if this help you!
Regards,
Omar
Enter below code in the above method it will cover catch block
if(Test.isRunningTest()){
throw new QueryException();
}