You need to sign in to do that
Don't have an account?
Mohan Selvam
Test class reached 69% need to reach 75%
Hi
Please guide me to reach 75% in my test class
My apex class as follows,
Public Class ProductTriggerHandler{
public void OnAfterUpdate(Map<Id,Product2> newProductMap,Map<Id,Product2> oldProductMap){
Boolean Checkin = false;
for(Product2 prod: newProductMap.values()) {
if(prod.cost__c!= oldProductMap.get(prod.id).cost__c){
Checkin = True;
}
}
if(Checkin == true){
UpdateGpPercentage(newProductMap,oldProductMap);
}
}
Private void UpdateGpPercentage(Map<Id,Product2> newProductMap,Map<Id,Product2> oldProductMap){
List<OpportunityLineItem> OppProductList = new List<OpportunityLineItem>([ Select id,opportunityid from OpportunityLineItem where PricebookEntry.Product2.id IN :newProductMap.Keyset() and opportunityid != null]);
List<Id> OppIds = new List<Id>();
List<Id> SSOppIds = new List<Id>();
for(OpportunityLineItem oli:OppProductList ){
OppIds.add(oli.opportunityid);
}
List<Opportunity> OppList = new List<Opportunity>();
List<Opportunity> OppListNew = new List<Opportunity>();
OppList = [Select id, Service_Sheet__c from Opportunity where id In :OppIds];
if(OppList.size() > 0){
For(Opportunity OPP : OppList){
if(OPP.Service_Sheet__c!=NULL){
SSOppIds.add(OPP.id);
}
}
if(SSOppIds.size() > 0){
Try{
Database.executeBatch(new UpdateOpportunityByBatch(SSOppIds));
} catch(Exception E){System.debug('Errors Msg'+E);}
}
}
}
}
My test class as follows
@IsTest(SeeallData=True)
Private class ProductTriggerHandlerTest{
Static TestMethod Void UpdateGpPercentageTest(){
Test.StartTest();
Product2 Ins_Prod = New Product2(Name='Test Product', ProductCode='DHI_6677_Test', Cost__c=5);
Insert Ins_Prod;
Product2 Prod =[Select Id, Cost__c from Product2 where ID=:Ins_Prod.id];
Prod.Cost__c=10.00;
Update Prod;
Opportunity Opp =[Select id,Service_Sheet__c From Opportunity Limit 1];
List<Id> OppIds = New List<id>();
OppIds.add(Opp.id);
Database.executeBatch(new UpdateOpportunityByBatch(OppIds));
Test.stopTest();
}
}
Note : Unerlined lines are not covered in my test class.. i got 69% ... please guide me to reach 75%. Lines covered 16/23
Please guide me to reach 75% in my test class
My apex class as follows,
Public Class ProductTriggerHandler{
public void OnAfterUpdate(Map<Id,Product2> newProductMap,Map<Id,Product2> oldProductMap){
Boolean Checkin = false;
for(Product2 prod: newProductMap.values()) {
if(prod.cost__c!= oldProductMap.get(prod.id).cost__c){
Checkin = True;
}
}
if(Checkin == true){
UpdateGpPercentage(newProductMap,oldProductMap);
}
}
Private void UpdateGpPercentage(Map<Id,Product2> newProductMap,Map<Id,Product2> oldProductMap){
List<OpportunityLineItem> OppProductList = new List<OpportunityLineItem>([ Select id,opportunityid from OpportunityLineItem where PricebookEntry.Product2.id IN :newProductMap.Keyset() and opportunityid != null]);
List<Id> OppIds = new List<Id>();
List<Id> SSOppIds = new List<Id>();
for(OpportunityLineItem oli:OppProductList ){
OppIds.add(oli.opportunityid);
}
List<Opportunity> OppList = new List<Opportunity>();
List<Opportunity> OppListNew = new List<Opportunity>();
OppList = [Select id, Service_Sheet__c from Opportunity where id In :OppIds];
if(OppList.size() > 0){
For(Opportunity OPP : OppList){
if(OPP.Service_Sheet__c!=NULL){
SSOppIds.add(OPP.id);
}
}
if(SSOppIds.size() > 0){
Try{
Database.executeBatch(new UpdateOpportunityByBatch(SSOppIds));
} catch(Exception E){System.debug('Errors Msg'+E);}
}
}
}
}
My test class as follows
@IsTest(SeeallData=True)
Private class ProductTriggerHandlerTest{
Static TestMethod Void UpdateGpPercentageTest(){
Test.StartTest();
Product2 Ins_Prod = New Product2(Name='Test Product', ProductCode='DHI_6677_Test', Cost__c=5);
Insert Ins_Prod;
Product2 Prod =[Select Id, Cost__c from Product2 where ID=:Ins_Prod.id];
Prod.Cost__c=10.00;
Update Prod;
Opportunity Opp =[Select id,Service_Sheet__c From Opportunity Limit 1];
List<Id> OppIds = New List<id>();
OppIds.add(Opp.id);
Database.executeBatch(new UpdateOpportunityByBatch(OppIds));
Test.stopTest();
}
}
Note : Unerlined lines are not covered in my test class.. i got 69% ... please guide me to reach 75%. Lines covered 16/23
Make sure to include all manadatory fields and proper data for the test object. Don't have practices of using SeeAllData= true in test classes
Thanks for your response.. still i am facing the same issue..
regards
S Mohan