I have a method in APEX class that does variety of checks via If else conditions with some SOQL statements. I created test data(Accounts, custom object records) to test the SOQL statements and it seems to go though fine. The place where my test class throws a null exception is when in my Apex code a new opportunity is created. For SOQL statements, I know we setup the test data before and then call the method that we need to test, but what about the case where my code is actually creating a new opportunity on real-time. To my knowledge, I don't need to setup anything for that, since it is a new record creation.
Here is the code inside my apex method that is causing trouble. It is fetching the record type id first that needs to be assigned to the new opportunity.
Id OpportunityRecordTypeId = Schema.getGlobalDescribe().get('Opportunity').getDescribe().getRecordTypeInfosByName().get(marketing.Lead_Type).getRecordTypeId(); Opportunity newopportunity = new Opportunity(); newopportunity.Contact__c=contact.id; newopportunity.AccountID=contact.AccountId; newopportunity.StageName= 'Prospecting'; newopportunity.RecordTypeID= OpportunityRecordTypeId; newopportunity.AccountId= contact.AccountId; newopportunity.Name='Final Test'; newopportunity.CloseDate=Date.parse('01/01/2019'); insert newopportunity;
It seems to throw the
System.NullPointerException: Attempt to de-reference a null object
error on the line where it fetches the record type id. How do I cater this my TEST class?