+ Start a Discussion
Money Care 7Money Care 7 

Code coverage improvement

Hi Everyone

I have wrtitten a test class for my apex class but i am able to coverage 65%.how to improve my code coverage ...
public with sharing class PicklistHandler 
{
    set<Id> setAccountId = new set<Id>();
    list<Account> lstAccount = new list<Account>();
  
    public void OnAfterInsert(list<Purchase_Order__c> lstDiscount) {
    
        for(Purchase_Order__c objDiscount : lstDiscount) {
        
            setAccountId.add(objDiscount.Company__c);
        }
        
        for(Account objAccount : [Select Id,Type From Account Where Id IN: setAccountId]) 
        {
            
            objAccount.Type = 'Customer';
            lstAccount.add(objAccount);
        }
        if(!lstAccount.isEmpty())
            update lstAccount;
    }
    
}
@isTest private class PicklistHandlerTestMethod
{
    static testMethod void TestPicklistHandler()
    {
        
   
    account a=new account(name='test',Region__c='East', Type='Prospect');
        insert a;
               Test.startTest();
        update a;
        Test.stopTest();
       
        
        Master_Product__c mp=new Master_Product__c(name='test name',Product_Code__c='c2');
        insert mp;
        
        contact c=new contact(firstname='Meenakshmi',lastname='Goswami',Designation__c='Developer',Accountid=a.id);
        insert c;


        Opportunity2__c op=new Opportunity2__c(Name='test1',Account__c=a.id,Master_Product__c=mp.id,Technical_Bid_date__c=date.Today(),Type_of_Business__c='Regular',Contact_Person__c=c.id);
        insert op;
        
        Opportunity_Product_Detail__c opd=new Opportunity_Product_Detail__c(Opportunity__c=op.id,Company__c=a.id,Quantity__c=10);
        insert opd;
        
        Quote__c qt=new Quote__c(Opportunity_Product_Detail__c=opd.id,Purpose_of_Sales__c='SEZ',Declaration_form_be_provided__c='No');
        insert qt;
        
        Purchase_Order__c sco=new Purchase_Order__c(Quote__c=qt.id,Opportunity__c=op.id);
        insert sco;
        Call_Up_Order__c cuo=new Call_Up_Order__c(name='test call up order',Quote__c=qt.id,Sale_Confirmation_Order__c=sco.id,Call_Up_Quantity__c=1);
        insert cuo;
        
        Invoice_Dispatch_details__c idd=new Invoice_Dispatch_details__c(Call_Up_Order__c=cuo.id,Purchase_Order__c=sco.id, Account_Lookup__c=a.id);
        insert idd;
        
        op.Approved_Artwork_Received__c=false;
        update op;
        
        
       
    }
}
User-added image

 
Best Answer chosen by Money Care 7
Dhanya NDhanya N
Hi Money Care,

In your test class add value to the field Company__c in Purchase_Order__c. So that your set setAccountId will not be blank.
Update like this:
Purchase_Order__c sco=new Purchase_Order__c(Quote__c=qt.id,Opportunity__c=op.id, Company__c = a.id);
insert sco;
Thanks,
Dhanya