You need to sign in to do that
Don't have an account?
Sheraz Hassan
I have problems creating test class
I have a following apex Class for which I need to create a test class
Following is my test class with only 29% code coverage
Can anyone please help me create a test class for my apex class & guide me on how to get to the section inside loop using test code.
public class GenerateFirstInvoices { @InvocableMethod public static void fetchOrderItems(List<Id> orderId) { List<OrderItem> oproduct = [SELECT Id, OrderId, Next_Billing_Date__c, OriginalOrderItemId, Payment_Term__c, Quantity, Remaining_Amount__c, Contact_Name__c, Contact_Address__c, Contact_Email__c, Contact_Number__c, Trn_Number__c, CRM_Number__c, FAR_or_HHS__c, Description, Created_Date__c FROM OrderItem WHERE OrderId = :orderId]; Proforma_Invoice__c[] recordsToInsert = new Proforma_Invoice__c[0]; Map<String, Integer> factors = new Map<String, Integer> { 'Monthly' => 11, '2 Months' => 5, 'Quarterly' => 3, '25%' => 3, 'Semi Annually' => 1, '50%' => 1 }; for (OrderItem oitem : oproduct) { Integer factor = factors.get(oitem.Payment_Term__c); Integer DateInterval = 0; for (Integer i = 1; i <= factor; i++) { if (factor == 11) { DateInterval = DateInterval + 30; } else if (factor == 5) { DateInterval = DateInterval + 60; } else if (factor == 3) { DateInterval = DateInterval + 90; } else if (factor == 1) { DateInterval = DateInterval + 180; } else { DateInterval = DateInterval; } recordsToInsert.add(new Proforma_Invoice__c( Order__c = oitem.OrderId, Order_Product__c = oitem.Id, Rate__c = oitem.Remaining_Amount__c / factor, Amount__c = oitem.Remaining_Amount__c / factor, Expense_Budget__c = oitem.Remaining_Amount__c / factor, Quantity__c = oitem.Quantity, Status__c = 'Draft', Paid_Status__c = 'No', Contact_Name__c = oitem.Contact_Name__c, Contact_Address__c = oitem.Contact_Address__c, Contact_Email__c = oitem.Contact_Email__c, Contact_Number__c = oitem.Contact_Number__c, TRN_Number__c = oitem.TRN_Number__c, FAR_or_HHS__c = oitem.FAR_or_HHS__c, CRM_No__c = oitem.CRM_Number__c, Created_Date__c = oitem.Created_Date__c + DateInterval )); } } insert recordsToInsert; } }
Following is my test class with only 29% code coverage
@isTest private class GenerateFirstInvoicesTEST { @isTest private static void fetchOrderItemsTEST() { String Payment_Terms = 'Monthly'; Database.SaveResult dsr; OrderItem drOrderItem; drOrderItem=new OrderItem(OrderId='802x00000000000AAA',PricebookEntryId='802x00000000000AAA',Quantity=123,Payment_Term__c='Monthly',type__c='Subscription'); dsr=Database.insert(drOrderItem,false); Test.startTest(); try{ GenerateFirstInvoices.fetchOrderItems(new List<Id>{dsr.getId()}); }catch(Exception e){} try{ GenerateFirstInvoices.fetchOrderItems(null); }catch(Exception e){} Test.stopTest(); Test.startTest(); try{ //drOrderItem.Payment_Term__c == 'Monthly'; Proforma_Invoice__c proforma = new Proforma_Invoice__c(); proforma.Rate__c = 100; proforma.Amount__c = 100; proforma.Order_Product__c = '802x00000000000AAA'; proforma.Quantity__c = 1; insert proforma; }catch(Exception e){} Test.stopTest(); } @isTest private static void GenerateFirstInvoicesTEST(){ GenerateFirstInvoices obj = new GenerateFirstInvoices(); } }
Can anyone please help me create a test class for my apex class & guide me on how to get to the section inside loop using test code.
Hi,
Please find the solution.
Please do some needful changes according to your requirement.
Please mark it as the Best Answer if it helps you.
Thank You.
All Answers
Hi,
Please find the solution.
Please do some needful changes according to your requirement.
Please mark it as the Best Answer if it helps you.
Thank You.
The test class code did not work from me even it went down to 0% coverage.
I have not clear idea of test classes so I copy your code and made the changes required, but no effect.
I will try a little more to make it exact 100%.
Also from your code I got a clear idea of how test class work.
Thank you for your help and support.