Here is my test bulkify test class and my actuall trigger handler methods. I dont know why but my code coverage is %76 and still it says test is not passed.. Is there anyone can help me about this situation.
@isTest
private class OrdLineItemTestClass {
static testMethod void myUnitTest() {
List<OrderProducts__c> BulkOli=new List<OrderProducts__c>();
List<OrderProducts__c> updateBulkOli=new List<OrderProducts__c>();
Order__c orderNew=new Order__c();
orderNew.Name='test-Order';
orderNew.CloseDate__c=system.Today();
orderNew.Stage='Prospecting';
Product2 productNew=new Product2();
ProductNew.Name='test-Product';
ProductNew.isActive=true;
insert productNew;
insert orderNew;
for(integer i=0 ; i<150; i++)
{
OrderProducts__c oli=new OrderProducts__c(
Products__c=ProductNew.Id,
Quantity__c=4,
SalesPrice__c=35000,
Name='test',
Order_mas__c=OrderNew.Id
);
BulkOli.add(oli);
}
test.startTest();
insert BulkOli;
for(OrderProducts__c InsertEquals: BulkOli)
{
system.assertEquals('test', oliEquals.Name);
}
for(OrderProducts__c updateOli: BulkOli)
{
OrderProducts__c oli=new OrderProducts__c(
Id=updateOli.Id,
Quantity__c=6
);
updateBulkOli.add(oli);
}
update updateBulkOli;
for(OrderProducts__c oliEquals: BulkOli)
{
system.assertEquals(6, oliEquals.Quantity__c);
}
delete BulkOli;
test.stopTest();
}
}
public class OrderLineItemProcess{
public OrderLineItemProcess(){}
public void onAfterInsert(Map<Id,OrderProducts__c> TriggerNew)
{
Map<Id, Order__c> ords = new Map<Id, Order__c>();
set<Id> productIds=new set<Id>();
Pricebook2 standartpb = [select id from Pricebook2 where IsStandard = true and isActive= true limit 1];
for(OrderProducts__c record: TriggerNew.values()) {
productIds.add(record.products__c);
ords.put(record.Id, null);
}
Map<Id, PricebookEntry> prices = new Map<Id,PricebookEntry>();
for(PricebookEntry pricebook: [Select Id,Product2Id,UnitPrice,Pricebook2Id From PricebookEntry Where Product2Id IN :productIds and Pricebook2Id=:standartpb.Id])
{
prices.put(pricebook.Product2Id,pricebook);
}
List<OpportunityLineItem> lineItems=new List<OpportunityLineItem>();
for(OrderProducts__c record: [select Products__c,order_mas__r.OpportunityLookup__c,Quantity__c,Discount__c,Salesprice__c, LineDescription__c,OppLineItemExternalId__c from OrderProducts__c where Id IN: TriggerNew.values()])
{
OpportunityLineItem oppLineItem=new OpportunityLineItem(
PricebookEntryId=prices.get(record.Products__c).Id,
OpportunityId=record.order_mas__r.OpportunityLookup__c,
Quantity=record.Quantity__c,
Discount=record.Discount__c,
UnitPrice=record.Salesprice__c,
Description=record.LineDescription__c,
OrdLineItemExternalId__c=record.Id
);
lineItems.add(oppLineItem);
}
FlaG.run=false;
insert(lineItems);
List<OrderProducts__c> updateOrdProductsList=new List<OrderProducts__c>();
for(OpportunityLineItem OppExternalId : lineItems)
{
OrderProducts__c Updateord = new OrderProducts__c(
Id=OppExternalId.OrdLineItemExternalId__c,
OppLineItemExternalId__c=OppExternalId.Id
);
updateOrdProductsList.add(Updateord);
}
update updateOrdProductsList;
}
public void onAfterUpdate(List<OrderProducts__c> TriggerNew)
{
List<OpportunityLineItem> oppList=new List<OpportunityLineItem>();
for(OrderProducts__c ordList : TriggerNew)
{
OpportunityLineItem opp=new OpportunityLineItem(
Id=ordlist.OppLineItemExternalId__c,
OpportunityId=ordList.order_mas__r.OpportunityLookup__c,
Quantity=ordList.Quantity__c,
Discount=ordList.Discount__c,
UnitPrice=ordList.Salesprice__c,
Description=ordList.LineDescription__c,
OrdLineItemExternalId__c=ordList.Id
);
oppList.add(opp);
}
if(FlaG.run)
{
FlaG.run=false;
update oppList;
}
}
public void onBeforeDelete(Map<Id,OrderProducts__c> oldMap)
{
List<OpportunityLineItem> opp=[Select Id,OrdLineItemExternalId__c from OpportunityLineItem Where OrdLineItemExternalId__c IN : oldMap.keyset() ];
for(OpportunityLineItem opp2 :opp)
{
if(FlaG.run)
{
delete opp2;
}
}
}
}
Attribution to: user7361
Possible Suggestion/Solution #1
Tests passing and test coverage are two different things. You will need to get all your tests passing and the coverage on average greater than 75% (and all your triggers above 0% coverage).
The try/catch in your test will hide any DMLExceptions and cause lines of code not to be covered. For example, if insert BulkOli
throws a DMLException, the 10 lines or so that follow before the catch will not be covered. Best to remove the try/catch altogether and fix any problems: this might be difficult but is necessary for your test to have value.
Note that this should never be done:
catch(DMLexception e){}
and generally letting exceptions propagate rather than trying to handle them is the right thing to do in tests and most other code too.
Using SeeAllData=true
and hard coded ids such as 01t20000003gOs6AAE
is a fragile approach because the referenced objects could be deleted or modified. It is better to create the objects as part of the test.
Ideally your test would contain more assert statements that check the results in more detail after each of the insert, update and delete operation. It is not unusual for a unit test to have more lines of code that the code it is testing.
Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/30224