You need to sign in to do that
Don't have an account?
test class for trigger
Hi All I wrote a trigger and test class for that but when i run a test its give me following error
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateOpportunitySOW: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.UpdateOpportunitySOW: line 17, column 8 | Class.TriggerTests.Scope_Of_Work: line 148, column 10 External entry point |
My trigger is
trigger UpdateOpportunitySOW on Scope_Of_Work__c (after insert, after update)
{
List<id> oppIds=new List<id>();
for (Scope_of_Work__c sow : trigger.new)
{
oppIds.add(sow.opportunity__c);
}
Map<Id, Opportunity> oppsById=new Map<Id, Opportunity>();
List<Opportunity> opps= [select id, SOW__c from Opportunity WHERE id in :oppIds];
oppsById.putAll(opps);
List<Opportunity> oppsToUpdate=new List<Opportunity>();
for (Scope_of_Work__c sow : trigger.new)
{
Opportunity opp=oppsById.get(sow.Opportunity__c);
opp.SOW__c = sow.id;
oppsToUpdate.add(opp);
}
update oppsToUpdate;
}
AND test class is
static testMethod void Scope_Of_Work()
{
Account acct = new Account(Name='Test Account', Type='Standard Account', Main_BTN__c='(123)456-7654', ShippingStreet='123 Test St', ShippingCity='Dallas', ShippingState='Tx', ShippingPostalCode='75050',
BillingStreet='123 Test St\napt300', BillingCity='Dallas', BillingState='Tx', BillingPostalCode='75050');
insert acct;
Opportunity o = new Opportunity(Name='Test Opp1', AccountId=acct.Id, Scope_of_Work__c='Test', StageName='New', Proposal_Issued_Date__c=Date.Today(),CloseDate=System.today());
insert o;
Contact cont = new Contact (FirstName='Contact', LastName='1', Phone='123-432-4356', AccountId=Acct.Id, Contact_Type__c='Finance/Credit;On-Site Coordinator');
insert cont;
Scope_Of_Work__c sow = new Scope_Of_Work__c(Products_Sold__c='VPN',IT_Contact__c= cont.Id);
insert Sow;
}
Proposal Button:
Your test data doesn't associate the sow with the test Opportunity. Thus list oppids is empty as is list opps.
You could find these problems by inserting system.debug(...) statements in your trigger
tryt this
Proposal button:
One more thing; your testMethod should do System.asserts to verify expected behavior -- just doing code coverage only ensures you don't have any exceptions. Although your example looks simple, you will no doubt expand it over time and having system.asserts will help you (or others) have a regression suite to ensure no change breaks the code.