You need to sign in to do that
Don't have an account?
HTANIRS
trigger test class code coverage issue
Hi,
Kindly help me in reaching 75% in code coverage. I am getting 45% for the below trigger. Kindly refer the Trigger and Test Class and let me know what change to achieve 75%.
Kindly help me in reaching 75% in code coverage. I am getting 45% for the below trigger. Kindly refer the Trigger and Test Class and let me know what change to achieve 75%.
Trigger ----- trigger insertOppsLineitem on OrderItems__c (after insert) { System.debug('--- Inside OrderItems Trigger ---'); Set<Decimal> orderIds = new Set<Decimal>(); for(OrderItems__c oi : Trigger.New) { orderIds.add(oi.OrderId__c); System.debug('--- Getting Custom Obj Id: ---' + orderIds); } Map<Decimal, List<Opportunity>> oppMap = new Map<Decimal, List<Opportunity>>(); if(!orderIds.isEmpty()){ System.debug('--- Checking Order ID: ---'); for(Opportunity opp : [SELECT Id, Name, OrderItem__c FROM Opportunity WHERE OrderItem__c IN: orderIds]) { if(!oppMap.containsKey(opp.OrderItem__c)) { oppMap.put(opp.OrderItem__c, new List<Opportunity> {opp}); System.debug('--- Opportunity Map ---' + oppMap); } } } List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1]; List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); for(OrderItems__c oi : Trigger.New){ System.debug('--- Order Items: ---' + oi); if(oppMap.containsKey(oi.OrderId__c)){ for(Opportunity opp : oppMap.get(oi.OrderId__c)) { System.debug('--- Inside Opportunity ---'); OpportunityLineItem oli = new OpportunityLineItem(); oli.OpportunityId = opp.Id; oli.PricebookEntryId = priceBookList[0].Id; oli.Quantity = oi.Qty_Ordered__c; oli.TotalPrice = oi.Price__c; oli.item_id__c = oi.Name; oli.Name__c = oi.Name__c; oliList.add(oli); System.debug('--- Inside Opportunity' + opp); System.debug('--- Oli List: ---'); } } } try { if(oliList.size()>0) { insert oliList; System.debug('--- Inserted Opp Line Items: ---'); System.debug('--- Inserted Opp Line Items: ---' + olilist.size()); } } catch(Exception e){ System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage()); } }
Test Class ---- @isTest private class insertOppsLineitemTest { static testMethod void testorderItems() { OrderItems__c oi = new OrderItems__c(); oi.Qty_Ordered__c = 1; oi.OrderId__c = 2333; oi.Price__c = 100; oi.Name = 'Test'; insert oi; OrderItems__c o = [SELECT Id, Name FROM OrderItems__c WHERE Name = 'Test']; Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('AccountRecordType').getRecordTypeId(); Account acc = new Account(); acc.Name = 'Test'; acc.recordtypeid = RecordTypeIdAccount; insert acc; Opportunity opp = new Opportunity(); opp.Name = 'Test'; opp.CloseDate = system.today(); opp.StageName = 'New'; opp.Account.Id = acc.Id; opp.OrderItem__c = oi.OrderId__c; insert opp; List<Opportunity> oppList = [SELECT Id, Name FROM Opportunity WHERE Name = 'Test']; List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1]; Id pricebookId = Test.getStandardPricebookId(); //Create your product Product2 prod = new Product2( Name = 'Product X', ProductCode = 'Pro-X', isActive = true ); insert prod; //Create your pricebook entry PricebookEntry pbEntry = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = prod.Id, UnitPrice = 100.00, IsActive = true ); insert pbEntry; OpportunityLineItem oli = new OpportunityLineItem(); oli.OpportunityId = opp.Id; oli.PricebookEntryId = pbEntry.Id; oli.Quantity = 1; oli.TotalPrice = 100; oli.item_id__c = '1254'; oli.Name__c = 'Testing'; insert oli; } }
Kindly check the below updated Trigger and Test Class. I am getting 58% code coverage.
Insert Method is not covering.
Thanks.
Kindly refer the below screenshot.
Thanks.