I'm trying to get code coverage for my trigger for when I deploy my trigger to production. This is my first time trying to provide code coverage to a Trigger in SalesForce, as I'm new to creating these kind of classes. I have the following trigger:
trigger UpdateOpportunity on Opportunity (after insert, before update){
Map<Id, Opportunity> recordTypeMap = new Map<Id, Opportunity>([SELECT RecordType.Name FROM Opportunity WHERE ID IN :Trigger.new]);
for(Opportunity opp : Trigger.new){
if(recordTypeMap.get(opp.Id).RecordType.Name == 'SGI Opportunity' && opp.StageName != 'Lead 0%'){
if(opp.Win_Reason__c == 'No Competition/Sole Source'){
opp.Competitor_s__c = 'No Competitor/Sole Source';
}
if(opp.Unsolicited_Bid__c == 'Yes'){
opp.Competitor_s__c = 'No Competitor/Sole Source';
}
}
}
}
For my test class, I have the following:
@isTest
private class UpdateOpportunityTest {
static testMethod void UpdateOpportunityTest() {
Id sgiOpportunityRecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'SGI Opportunity'].Id;
Opportunity opportunity = new Opportunity(
Name = 'Test Opportunity', Opportunity_Type__c = 'Add-On',
Product_Revenue_Type__c = '% of Completion', CloseDate = Date.today(), StageName = 'Lead 0%',
CurrencyIsoCode = 'USD', Opportunity_Intergaph_Line_of_Business__c = 'Government', Solution__c = 'Border Security',
OwnerId = UserInfo.getUserId(), RecordTypeId = sgiOpportunityRecordTypeId);
insert opportunity;
Test.startTest();
try {
insert opportunity;
}
catch (DmlException ex) {
System.assertEquals(StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION, ex.getDmlType(0));
System.assertEquals('Cannot delete a Contact with associated Opportunities.', ex.getDmlMessage(0));
}
Test.stopTest();
}
}
And I get the following error when I try and run the test:
Error Message System.AssertException: Assertion Failed: Expected: FIELD_CUSTOM_VALIDATION_EXCEPTION, Actual: INVALID_FIELD_FOR_INSERT_UPDATE
Stack Trace Class.UpdateOpportunityTest.UpdateOpportunityTest: line 21, column 1
I'm lost at to what I'm doing wrong with this TEST class.
Any help is greatly appreciated! Thanks for your time in advance!