+ Start a Discussion
Sheraz HassanSheraz Hassan 

I have problems creating test class

I have a following apex Class for which I need to create a test class
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.
Best Answer chosen by Sheraz Hassan
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

@isTest

public class orderproductTestClass {
    
    public static testMethod void testorderproduct(){

    // Insert Account

    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  pricebookId ;
	o.Unlock_Order__c ='fill your data';//fill according to datatype
    
    insert o;
	
	 List<Id> oList=new List<>(Id);
	 oList.add(o.id);
	
	OrderItem i = new OrderItem();
    i.OrderId = o.id;
    i.Quantity = 24;
    i.UnitPrice = 240;
    i.Product2id = p.id;
    i.PricebookEntryId=standardPrice.id;
	i.Payment_Term__c='Monthly';
	i.Remaining_Amount__c ='500';
	i.Contact_Name__c='Test';
	i.Contact_Address__c='New Delhi';
	i.Contact_Email__c='abc@gmail.com';
	i.Contact_Number__c='1234554321';
	i.TRN_Number__c='123';
	i.FAR_or_HHS__c='Data';
	i.CRM_Number__c='Data 2';
	i.Created_Date__c =Date.Today();
    insert i;
	
	        Test.startTest();
			GenerateFirstInvoices.fetchOrderItems(oList);
            Test.stopTest();

	}
	}


Please do some needful changes according to your requirement.

Please mark it as the Best Answer if it helps you.

Thank You.

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

@isTest

public class orderproductTestClass {
    
    public static testMethod void testorderproduct(){

    // Insert Account

    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  pricebookId ;
	o.Unlock_Order__c ='fill your data';//fill according to datatype
    
    insert o;
	
	 List<Id> oList=new List<>(Id);
	 oList.add(o.id);
	
	OrderItem i = new OrderItem();
    i.OrderId = o.id;
    i.Quantity = 24;
    i.UnitPrice = 240;
    i.Product2id = p.id;
    i.PricebookEntryId=standardPrice.id;
	i.Payment_Term__c='Monthly';
	i.Remaining_Amount__c ='500';
	i.Contact_Name__c='Test';
	i.Contact_Address__c='New Delhi';
	i.Contact_Email__c='abc@gmail.com';
	i.Contact_Number__c='1234554321';
	i.TRN_Number__c='123';
	i.FAR_or_HHS__c='Data';
	i.CRM_Number__c='Data 2';
	i.Created_Date__c =Date.Today();
    insert i;
	
	        Test.startTest();
			GenerateFirstInvoices.fetchOrderItems(oList);
            Test.stopTest();

	}
	}


Please do some needful changes according to your requirement.

Please mark it as the Best Answer if it helps you.

Thank You.

This was selected as the best answer
Sheraz HassanSheraz Hassan
Hi Suraj, thank you for your help.
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.
Sheraz HassanSheraz Hassan
Hello suraj, I made some ammendments into data and changed a line or two in code. Now it is working and giving me 80% of code coverage.
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.