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

Test coverage for .addError and .add statments in trigger
Hi can any one help me out to get test coverage for .addError and .add statments
Below is the trigger and test calss.
Bold line are not covered with the existing test class.
trigger Erroriftwoopportunityareopen on Opportunity (before insert) {
Set <id> setAccWithProspectingOpp = new Set <Id>();//Varaible to hold Account with Prospecting opp
Set <id> setAccId = new Set <Id>();//Varaible to hold Account IDs
if(trigger.isbefore){
if(trigger.isinsert){
//Iterate through opportunity
for(Opportunity o:Trigger.new){
//System.debug('Account Id -->'+o.Accountid);
//Verify account id not null and opportunity stagename is Prospecting
if(o.Accountid != Null && o.StageName == 'Prospecting'){
setAccId.add(o.Accountid);//Account is added to setAccId(Varaible)
}
}
//System.debug('Set of accounts -->'+setAccId);
//Fetch the accounitid of opportunity from aetAccId(Varaible)
for(Opportunity Opp : [Select id,accountid from opportunity where accountid in :setAccId]){
setAccWithProspectingOpp.add(Opp.accountid);//Add opportunity account id in setAccWithProspectingOpp(Variable)
}
//System.debug('Set of accounts with Prosp Opp-->'+setAccWithProspectingOpp);
//Iterate through opportunity object
for(Opportunity o:Trigger.new){
//Chack condition that in setAccWithProspectingOpp(Varaible)contains opportunity account id
if(setAccWithProspectingOpp.contains(o.accountid))
o.addError('This account already has Opportunity with StageName as Prospecting');
}
}
}
}
Test class
@isTest
public class ErroriftwoopportunityareopenTest{
static testMethod void Test(){
User thisUser = [ Select Id from User where Id = :UserInfo.getUserId() ];
Account acc = new Account(Name = 'TestAccount',Rating = 'Hot',Type = 'Prospect');
//update acc;
Opportunity opp = new Opportunity(Name = 'TestOpp',Accountid = acc.Id,CloseDate = Date.Today(),StageName = 'Prospecting');
//update opp;
Opportunity opp1 = new Opportunity(Name = 'TestOpp1',Accountid = acc.Id,CloseDate = Date.Today(),StageName = 'ClosedWon');
//update opp1;
Opportunity opp2 = new Opportunity(Name = 'TestOpp2',Accountid = acc.Id,CloseDate = Date.Today(),StageName = 'ClosedLost');
//update opp2;
Task t = new Task(WhatId=acc.Id,WhoId = opp.Id,Subject = 'Email',Status = 'Completed', Priority ='Normal');
insert t;
Test.startTest();
insert acc;
insert opp;
insert opp1;
insert opp2;
User someOtherUser = [Select Id From User Where Id != :UserInfo.getUserId() And isActive = TRUE LIMIT 1];
system.runAs(someOtherUser){
insert opp;
}
Test.StopTest();
}
}
Opportunity opp = new Opportunity(Name = 'TestOpp',Accountid = acc.Id,CloseDate = Date.Today(),StageName = 'Prospecting');
at the time you are inserting above opportunity, there are no any other opportunities in DB.
try inserting opp1 before opp
All Answers
place the insert acc; just after you initialized acc object. before initializing opportunities.
to cover add error, you will have to have a try catch block in your test clas. otherwise test will fail as exception is thrown from add error
Hi,
followed as you said now i got only 83% coverage
and now
setAccWithProspectingOpp.add(Opp.accountid);//Add opportunity account id in setAccWithProspectingOpp(Variable)o.addError('This account already has Opportunity with StageName as Prospecting');
above two line are not covered.
Opportunity opp = new Opportunity(Name = 'TestOpp',Accountid = acc.Id,CloseDate = Date.Today(),StageName = 'Prospecting');
at the time you are inserting above opportunity, there are no any other opportunities in DB.
try inserting opp1 before opp
Assigned Kodos for u.
Hi ,
A quick question
can you please let me know how to get code coverage forbelow line
If(setOppIds.size()>0){
//Fetch list of task associated with opportunity stored in setOppIds varaible
lsttask = [Select id, Status from Task where WhatId in :setOppIds];
if(lsttask.size()>0){
for(Task tsk : lsttask){
tsk.Status = 'Completed';
}
update lsttask;
}
}
Test code
:i had written is
Opportunity opp2 = new Opportunity(Name = 'TestOpp2',Accountid = acc.Id,CloseDate = Date.Today(),StageName = 'ClosedLost');
insert opp2;
Task t = new Task(Subject = 'Sample Test',WhatId=opp2.Id,Type = 'Email',Status = 'Completed', Priority ='Normal',ActivityDate =Date.Today());
update t;
can you please help me out this.
update t; should be insert t;.
if your code is from opportunity trigger, check whether trigger is marked for after update. and update the opportunity after inserting task
Hi,
Modified code as u said but still task realted cose was not covered.
post the full code of the trigger
Full trigger code
when inserting opp2 set stageName to something other than "Closed Lost"
when updating change it to "Closed Lost"