+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Test Class Coverage (Already 60% is Covered)

Hi Friends,

I am Trying to cover a test class for below Trigger , It's covered with 60%. Can anyone please help to cover upto 100%

@Trigger
/*
        Description: Update Preferred Price & Least Price in Indent Object
        Owner: Thabi
        Date: 17-Sep-13
        Test Class: TestOI_Indent_Creation
*/
trigger OI_UpdateIndentPrice on RawMaterial__c (after update) {
    map<id, RawMaterial__c> mapRm = new map<id,RawMaterial__c>(); 
    list<Indent__c> lstupdateindent = new list<Indent__c>();

    for(RawMaterial__c irow: trigger.new){
        if( irow.Preferred_Vendor__c != null || irow.Least_Vendor__c != null){
            mapRm.put(irow.id,irow);
        }
    }

    List<Indent__c> lstIndent = [select id, Raw_Material__c, PV_Rm_Cost__c from Indent__c
                                  where Raw_Material__c IN:mapRm.keyset() AND Product__c != NULL];
    
    if(lstIndent.size()>0){
        for(Indent__c krow : lstIndent){
            //Below Lines are not covered
            if(mapRm.containskey(krow.Raw_Material__c)){
                krow.PV_Rm_Cost__c = mapRm.get(krow.Raw_Material__c).Preferred_Price__c;
                krow.LV_Rm_Cost__c = mapRm.get(krow.Raw_Material__c).Least_Price__c;  
                lstupdateindent.add(krow);      
            }  
        
        }
        
        update lstupdateindent;
    }

}


@Test Class - I covered almost 60%
 
@IsTest
Public Class OI_RollupMaterialCost_Test{

static testMethod void RollupMaterialCostTest(){

Product2 prod = New Product2();
prod.Name = 'Test Product';
prod.HSN_Code__c = 'Prod123';
prod.Total_Material_Cost__c = 50;
prod.Total_RM_Cost__c = 100;
insert prod;

Vendor__c vr = New Vendor__c();
vr.Name = 'Test Name';
insert vr;

Vendor__c vr1 = New Vendor__c();
vr.Name = 'Test Name1';
insert vr1;

RawMaterial__c rm = New RawMaterial__c();
rm.Name = 'Test_rawMaterials';
rm.Unit__c = 'Bag';
rm.Preferred_Vendor__c = vr.Id;
rm.Preferred_Price__c =10;
rm.Least_Price__c = 20;
rm.Least_Vendor__c  = vr1.Id;
insert rm;

Indent__c ind = New Indent__c();
ind.Measurement__c = 12;
ind.PV_Rm_Cost__c =rm.Preferred_Price__c;
ind.LV_Rm_Cost__c = rm.Least_Price__c ;
ind.Product__c = prod.Id;
ind.Raw_Material__c = rm.Id;
ind.PV_Rm_Cost__c = 26;
ind.LV_Rm_Cost__c = 36;
Insert ind;

ind.Measurement__c = 14;
ind.PV_Rm_Cost__c =rm.Preferred_Price__c;
ind.LV_Rm_Cost__c = rm.Least_Price__c ;
ind.Product__c = prod.Id;
ind.Raw_Material__c = rm.Id;
Update ind;
}

}

Thanks in advance !!

Regards,
Soundar. 

 
Best Answer chosen by Soundar Raj Ponpandi
Balayesu ChilakalapudiBalayesu Chilakalapudi
Add these two lines in your test method
rm.Least_Price__c = 20;
rm.Least_Vendor__c  = vr1.Id;
update rm;

All Answers

Balayesu ChilakalapudiBalayesu Chilakalapudi
Add these two lines in your test method
rm.Least_Price__c = 20;
rm.Least_Vendor__c  = vr1.Id;
update rm;
This was selected as the best answer
Soundar Raj PonpandiSoundar Raj Ponpandi
Hi Balayesu Chilakalapudi,

Thanks For your quick response, I have updated rawmaterial with below code . Even it's showing 57% with covered.

@Test_Class
 
@IsTest
Public Class OI_RollupMaterialCost_Test{

static testMethod void RollupMaterialCostTest(){

Product2 prod = New Product2();
prod.Name = 'Test Product';
prod.HSN_Code__c = 'Prod123';
prod.Total_Material_Cost__c = 50;
prod.Total_RM_Cost__c = 100;
insert prod;

Vendor__c vr = New Vendor__c();
vr.Name = 'Test Name';
insert vr;

Vendor__c vr1 = New Vendor__c();
vr.Name = 'Test Name1';
insert vr1;

RawMaterial__c rm = New RawMaterial__c();
rm.Name = 'Test_rawMaterials';
rm.Unit__c = 'Bag';
rm.Preferred_Vendor__c = vr.Id;
rm.Preferred_Price__c =10;
rm.Least_Price__c = 20;
rm.Least_Vendor__c  = vr1.Id;
insert rm;

rm.Least_Price__c = 20;
rm.Least_Vendor__c  = vr1.Id;
rm.Preferred_Vendor__c = vr.Id;
update rm;

Indent__c ind = New Indent__c();
ind.Measurement__c = 12;
ind.PV_Rm_Cost__c =rm.Preferred_Price__c;
ind.LV_Rm_Cost__c = rm.Least_Price__c ;
ind.Product__c = prod.Id;
ind.Raw_Material__c = rm.Id;
ind.PV_Rm_Cost__c = 26;
ind.LV_Rm_Cost__c = 36;
Insert ind;

ind.Measurement__c = 14;
ind.PV_Rm_Cost__c =rm.Preferred_Price__c;
ind.LV_Rm_Cost__c = rm.Least_Price__c ;
ind.Product__c = prod.Id;
ind.Raw_Material__c = rm.Id;
Update ind;
}

}

Regards,
Soundar.
Soundar Raj PonpandiSoundar Raj Ponpandi
Hi Balayesu Chilakalapudi,

Your suggestion is absolutely correct, Before i was update a raw material in before update. Right now i have changed my issue (Updated raw material after insert the indent).
 
@IsTest
Public Class OI_RollupMaterialCost_Test{

static testMethod void RollupMaterialCostTest(){

Product2 prod = New Product2();
prod.Name = 'Test Product';
prod.HSN_Code__c = 'Prod123';
prod.Total_Material_Cost__c = 50;
prod.Total_RM_Cost__c = 100;
insert prod;

Vendor__c vr = New Vendor__c();
vr.Name = 'Test Name';
insert vr;

Vendor__c vr1 = New Vendor__c();
vr.Name = 'Test Name1';
insert vr1;

RawMaterial__c rm = New RawMaterial__c();
rm.Name = 'Test_rawMaterials';
rm.Unit__c = 'Bag';
rm.Preferred_Vendor__c = vr.Id;
rm.Preferred_Price__c =10;
rm.Least_Price__c = 20;
rm.Least_Vendor__c  = vr1.Id;
insert rm;

rm.Least_Price__c = 20;
rm.Least_Vendor__c  = vr1.Id;
rm.Preferred_Vendor__c = vr.Id;


Indent__c ind = New Indent__c();
ind.Measurement__c = 12;
ind.PV_Rm_Cost__c =rm.Preferred_Price__c;
ind.LV_Rm_Cost__c = rm.Least_Price__c ;
ind.Product__c = prod.Id;
ind.Raw_Material__c = rm.Id;
ind.PV_Rm_Cost__c = 26;
ind.LV_Rm_Cost__c = 36;
Insert ind;

ind.Measurement__c = 14;
ind.PV_Rm_Cost__c =rm.Preferred_Price__c;
ind.LV_Rm_Cost__c = rm.Least_Price__c ;
ind.Product__c = prod.Id;
ind.Raw_Material__c = rm.Id;
Update ind;

update rm;




}

}

Thanks.