You need to sign in to do that
Don't have an account?

Test class for an opportunity stage change trigger
Hi, I am new as a Developer and have just written a trigger. Could somebody please help me write test calsses for the same?
Here goes the Trigger:
trigger OpportunityUpdResigned on Account ( after update) {
set<Id> setAccountId = new set<Id>();
for(Account a : Trigger.new){
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c )
{
if (a.Fund_Date__c == null ||a.Assets_Under_Management__c ==null){
setAccountId.add(a.Id);
}
}
}
List<Opportunity> lstOpportunity = [SELECT Id, StageName FROM Opportunity WHERE AccountId IN: setAccountId];
for(Opportunity objOpp: lstOpportunity)
{
if(objOpp.StageName == 'Closed Lost'){
objOpp.StageName = 'Installed';
objOpp.Closed_Lost_Reason__c = null;
}
}
update lstOpportunity;
Here goes the Trigger:
trigger OpportunityUpdResigned on Account ( after update) {
set<Id> setAccountId = new set<Id>();
for(Account a : Trigger.new){
if(a.Resigned_Date__c != trigger.oldMap.get(a.Id).Resigned_Date__c )
{
if (a.Fund_Date__c == null ||a.Assets_Under_Management__c ==null){
setAccountId.add(a.Id);
}
}
}
List<Opportunity> lstOpportunity = [SELECT Id, StageName FROM Opportunity WHERE AccountId IN: setAccountId];
for(Opportunity objOpp: lstOpportunity)
{
if(objOpp.StageName == 'Closed Lost'){
objOpp.StageName = 'Installed';
objOpp.Closed_Lost_Reason__c = null;
}
}
update lstOpportunity;
@isTest
public class OppUpResigned2Test {
Public static testMethod void TestOppUpdResigned2(){
Account AccObj = new Account();
AccObj.Name = 'Test';
AccObj.Fund_Date__c = Date.newInstance(2019, 06, 9);
AccObj.Resigned_Date__c = Date.newInstance(2019, 06, 12);
Insert AccObj;
Opportunity OppObj = new Opportunity();
OppObj.Name = 'Testt';
OppObj.StageName = 'Closed Lost';
OppObj.CloseDate = Date.newInstance(2019, 12, 31);
OppObj.AccountId = AccObj.id;
Insert OppObj;
Account AccObj2 = new Account();
AccObj2.Name = 'Test2';
AccObj2.Fund_Date__c = Date.newInstance(2019, 06, 6);
AccObj2.Assets_Under_Management__c = 100;
Insert AccObj2;
Opportunity OppObj2 = new Opportunity();
OppObj.Name = 'Testt2';
OppObj2.StageName = 'Closed Lost';
OppObj2.CloseDate = Date.newInstance(2019, 12, 31);
OppObj2.AccountId = AccObj2.id;
Insert OppObj2;
Test.startTest();
AccObj.Resigned_Date__c = null;
update AccObj;
AccObj2.Assets_Under_Management__c = null;
update AccObj2;
Test.stopTest();
}
}
The code is giving 0% coverage and this is the error that I am getting:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]