I need to write a unit test for a class that I have created. Mostly based on Opportunities and OpportunityLineItem.
For some reason, I cannot get coverage past my for loop. I have tried everything, but I simply do not understand why I cannot seem to get coverage past my for loop.
For reference, my for loop is the last bit of code that is highlighted in blue. Everything else past my for loop is in red. Does this mean that I am not getting past my for loop? Have I correctly created OpportunityLineItem in my test class?
I need some help in understanding how to fix my current test class. I am very new to unit testing, so I am really looking to understand more than anything. Any help is appreciated. thank you.
This is a portion of my current class:
Public class TriggerHandler{
//public lists to hold lists based on cancellation status and opportunity line items
Public static List<OpportunityLineItem> lstOppProd = new List<OpportunityLineItem>();
Public static List<Contact> lstContactsToProcess = new List<Contact>();
public static void processOpps(List<Opportunity> newOpp){
//map helper of Opportunities based on cancelled status
Map<Id, Opportunity> mapCancelledSubs = New Map<Id, Opportunity>([SELECT id From Opportunity Where Opportunity.Id in :newOpp
AND Order_Status__c IN ('Cancelled','Expired','Pending Cancellation')
And Type ='Subscription' And (Not Coupon_Codes__c Like '%test%')]);
//list of OpportunityLineItems tied to newOpp
list<OpportunityLineItem> lstOppProd = [Select id, OpportunityId, ContactId__c, ProductCode From OpportunityLineItem
Where OpportunityId In (Select Opportunity.Id From Opportunity Where Id In :newOpp AND
Type = 'Subscription'And AccountId != Null)
And OrderItemType__c = 'Line Item'];
for(OpportunityLineItem oppProd : lstOppProd){
List<Object> paramSurveyUrl = new List<Object>{oppProd.ContactId__c,oppProd.OpportunityId, oppProd.ProductCode, myDate};
List<Object> paramProdFieldAbbv = new List<Object>{productAbbreviations.get(oppProd.ProductCode)};
This is my test class:
@isTest
public class TestTriggerHandler {
//testing trigger by passing lists, and creating Test Opportunities and Accounts
static testmethod void afterUpdateTestMethod1(){
List<OpportunityLineItem> lstOppProd = new List<OpportunityLineItem>();
List<Contact> lstContactsToProcess = new List<Contact>();
//create Test Opportunity
Opportunity opp1 = new Opportunity(Name='test opp', StageName='stage', Probability = 95, CloseDate=system.today(), Order_Status__c = 'Pending');
insert opp1;
opp1.Amount = 1000;
opp1.StageName = 'Closed Won';
update opp1;
///create Account
Id accRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
Account Accnt = new Account(RecordTypeID = accRecordTypeId,FirstName = 'Test FName 1',LastName = 'Test LName 1',PersonEmail ='[email protected]',
PersonMailingStreet = 'Test Street 01',PersonMailingPostalCode = '12345',PersonMailingCity = 'City',PersonHomePhone = '3456789876',PersonMobilePhone = '1234567897',
Contact_Stage__pc = '75 - Raving Fan', Type ='Customer');
try{insert Accnt;}
catch(Exception e){system.debug(e.getMessage() + e.getLineNumber() + e.getTypeName());}
//creating opportunity line item, and product
Id pricebookId = Test.getStandardPricebookId();
Product2 prd1 = new Product2 (Name='Test Product',Description='Test Product Entry 1', productCode = '302541', isActive = true);
insert prd1;
PricebookEntry pbe1 = new PricebookEntry (Product2ID = prd1.id, Pricebook2ID = pricebookId, UnitPrice = 247, isActive = true);
insert pbe1;
//create Test Opportunity
Opportunity opp = new Opportunity(Name='test opp', AccountId = Accnt.Id, StageName='Closed-Won', CloseDate=system.today(), Order_Status__c = 'Cancelled', Type = 'Subscription');
update opp;
OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID = opp.id, ContactId__c = Accnt.PersonContactId, Quantity=1, TotalPrice = 247, OrderItemType__c = 'Line Item');
insert lineItem1;
opp.Order_Status__c = 'Active';
try{update opp;}
catch(Exception e){system.debug(e.getMessage() + e.getLineNumber() + e.getTypeName());}
}
insert Acct;
andupdate opp;
. Getting exceptions in a test method is very much something that you want to know about, and printing the exception to the debug log and swallowing the exception just makes it harder to have any issues brought to your attention.