You need to sign in to do that
Don't have an account?
Dolgoldy
Help for test class on triggerhandler
Hi all,
Here is my trigger and trigger handler on opprtunity and test class.
In test class i am facing error as "Method does not exist or incorrect signature: void UpdateArrOPP(List<SObject>, Map<Id,SObject>) from the type opportunityArrTriggerHandler"
can anyone help me sort out this.
////Trigger
Trigger ArrOpptrigger on Opportunity (after update) {
if(trigger.isUpdate){
if(trigger.isAfter)
{
if(!PreventOppArrRecurssion.firstcall){
PreventOppArrRecurssion.firstcall =false;
opportunityArrTriggerHandler.UpdateArrOPP(trigger.new,trigger.oldmap);
}
}
}
}
///Handler class
public class opportunityArrTriggerHandler {
public static void UpdateArrOPP(list<opportunity>opplist,map<Id,Opportunity>oldoppmap)
{
List <ARRCalcualateOpportunity__c> ArroppList = new List <ARRCalcualateOpportunity__c>();
for (Opportunity oppobj : opplist)
{
if (oppobj.StageName == 'Closed Won')
{
for(OpportunityLineItem Oli:[select Id,Name,Product2Id,Product2.Family from
OpportunityLineItem where OpportunityId=:oppobj.Id])
{
if(oli.Product2.Family =='Software Recurring')
{
// To display date in asscending order
Date dt = oppobj.closedate;
for(Integer i =0;i<12;i++)
{
ARRCalcualateOpportunity__c arrObj = new ARRCalcualateOpportunity__c();
arrobj.ARRdate__c = oppobj.CloseDate;
//will display 12 date in ascending order
arrobj.ARRdate__c =dt.addMonths(i);
arrobj.Name = oppobj.name;
arrobj.oppAmount__c = oppobj.Amount;
List<OpportunityLineItem> oppli= [select Id from OpportunityLineItem
where OpportunityId = : oppobj.Id];
arrobj.Opportunity_Product__c = oppli[0].id;
arrobj.OpportunityArr__c= oppobj.Id;
ArroppList.add(arrobj);
}
}
}
}
}
insert ArroppList;
}
}
///Test class
@isTest
public class OpportunityTriggerHnadlerTest1 {
testmethod static void UpdateArrOPP1(){
// set<string> SetOpportunityId = new set<string>();
// date tempdte;
list<Opportunity>oppList=new list<Opportunity>();
map<Id,Opportunity>oldoppmap= new map<Id,Opportunity>();
Product2 prod = new Product2(Name = 'Anti-infectives 2007', Family = 'software Recurring', IsActive = true);
insert prod;
/*Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
insert standardPB;*/
Pricebook2 standardPricebook = new Pricebook2(
Id = Test.getStandardPricebookId(),
IsActive = true);
update standardPricebook;
//System.debug('Pricebook'+standardPricebook);
Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book', Description = 'Price Book 2009 Products',
IsActive = true);
insert pb;
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPricebook.Id, Product2Id = prod.Id,
UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
insert standardPrice;
/*PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
insert standardPrice;*/
Opportunity oppobj=new Opportunity();
oppobj.Name='Testing Opportunity';
oppobj.CloseDate=system.today();
oppobj.StageName = 'Prospecting';
// oppobj.IsClosed_won__c=true;
//SetOpportunityId.add(oppobj.Id);
insert oppobj;
List<OpportunityLineItem> lstOppLineItem=new List<OpportunityLineItem>();
OpportunityLineItem olli = new OpportunityLineItem (Quantity=2, OpportunityId=oppobj.Id, TotalPrice=10, PriceBookEntryId=standardPrice.Id);
lstOppLineItem.add(olli);
insert lstOppLineItem;
// tempdte = olli.Opportunity.CloseDate;
oppobj.StageName='Closed Won';
//oppobj.IsClosed_won__c=false;
update oppobj;
List <ARRCalcualateOpportunity__c> ArroppList = new List <ARRCalcualateOpportunity__c>();
Date dt = oppobj.closedate;
for(Integer i =0;i<12;i++){
ARRCalcualateOpportunity__c arrObj = new ARRCalcualateOpportunity__c();
arrobj.ARRdate__c = oppobj.CloseDate;
// obj.Name =String.valueOf(tempdte);
//objlist.add(obj);
arrobj.ARRdate__c =dt.addMonths(i);
arrobj.Name = oppobj.name;
arrobj.oppAmount__c = oppobj.Amount;
List<OpportunityLineItem> oppli= [select Id from OpportunityLineItem
where OpportunityId = : oppobj.Id];
arrobj.Opportunity_Product__c = oppli[0].id;
arrobj.OpportunityArr__c= oppobj.Id;
ArroppList.add(arrobj);
}
insert arropplist;
Test.startTest();
//TriggerHandler_opportunity closedTrigger=new TriggerHandler_opportunity();
//TriggerHandler_opportunity.CreateRecord(setopportunityId);
// TriggerHandler_opportunity.updateopprecord(setopportunityId);
PreventOppArrRecurssion.firstcall =false;
opportunityArrTriggerHandler.UpdateArrOPP(trigger.new,trigger.oldmap);
Test.stopTest();
}
}
Here is my trigger and trigger handler on opprtunity and test class.
In test class i am facing error as "Method does not exist or incorrect signature: void UpdateArrOPP(List<SObject>, Map<Id,SObject>) from the type opportunityArrTriggerHandler"
can anyone help me sort out this.
////Trigger
Trigger ArrOpptrigger on Opportunity (after update) {
if(trigger.isUpdate){
if(trigger.isAfter)
{
if(!PreventOppArrRecurssion.firstcall){
PreventOppArrRecurssion.firstcall =false;
opportunityArrTriggerHandler.UpdateArrOPP(trigger.new,trigger.oldmap);
}
}
}
}
///Handler class
public class opportunityArrTriggerHandler {
public static void UpdateArrOPP(list<opportunity>opplist,map<Id,Opportunity>oldoppmap)
{
List <ARRCalcualateOpportunity__c> ArroppList = new List <ARRCalcualateOpportunity__c>();
for (Opportunity oppobj : opplist)
{
if (oppobj.StageName == 'Closed Won')
{
for(OpportunityLineItem Oli:[select Id,Name,Product2Id,Product2.Family from
OpportunityLineItem where OpportunityId=:oppobj.Id])
{
if(oli.Product2.Family =='Software Recurring')
{
// To display date in asscending order
Date dt = oppobj.closedate;
for(Integer i =0;i<12;i++)
{
ARRCalcualateOpportunity__c arrObj = new ARRCalcualateOpportunity__c();
arrobj.ARRdate__c = oppobj.CloseDate;
//will display 12 date in ascending order
arrobj.ARRdate__c =dt.addMonths(i);
arrobj.Name = oppobj.name;
arrobj.oppAmount__c = oppobj.Amount;
List<OpportunityLineItem> oppli= [select Id from OpportunityLineItem
where OpportunityId = : oppobj.Id];
arrobj.Opportunity_Product__c = oppli[0].id;
arrobj.OpportunityArr__c= oppobj.Id;
ArroppList.add(arrobj);
}
}
}
}
}
insert ArroppList;
}
}
///Test class
@isTest
public class OpportunityTriggerHnadlerTest1 {
testmethod static void UpdateArrOPP1(){
// set<string> SetOpportunityId = new set<string>();
// date tempdte;
list<Opportunity>oppList=new list<Opportunity>();
map<Id,Opportunity>oldoppmap= new map<Id,Opportunity>();
Product2 prod = new Product2(Name = 'Anti-infectives 2007', Family = 'software Recurring', IsActive = true);
insert prod;
/*Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
insert standardPB;*/
Pricebook2 standardPricebook = new Pricebook2(
Id = Test.getStandardPricebookId(),
IsActive = true);
update standardPricebook;
//System.debug('Pricebook'+standardPricebook);
Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book', Description = 'Price Book 2009 Products',
IsActive = true);
insert pb;
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPricebook.Id, Product2Id = prod.Id,
UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
insert standardPrice;
/*PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
insert standardPrice;*/
Opportunity oppobj=new Opportunity();
oppobj.Name='Testing Opportunity';
oppobj.CloseDate=system.today();
oppobj.StageName = 'Prospecting';
// oppobj.IsClosed_won__c=true;
//SetOpportunityId.add(oppobj.Id);
insert oppobj;
List<OpportunityLineItem> lstOppLineItem=new List<OpportunityLineItem>();
OpportunityLineItem olli = new OpportunityLineItem (Quantity=2, OpportunityId=oppobj.Id, TotalPrice=10, PriceBookEntryId=standardPrice.Id);
lstOppLineItem.add(olli);
insert lstOppLineItem;
// tempdte = olli.Opportunity.CloseDate;
oppobj.StageName='Closed Won';
//oppobj.IsClosed_won__c=false;
update oppobj;
List <ARRCalcualateOpportunity__c> ArroppList = new List <ARRCalcualateOpportunity__c>();
Date dt = oppobj.closedate;
for(Integer i =0;i<12;i++){
ARRCalcualateOpportunity__c arrObj = new ARRCalcualateOpportunity__c();
arrobj.ARRdate__c = oppobj.CloseDate;
// obj.Name =String.valueOf(tempdte);
//objlist.add(obj);
arrobj.ARRdate__c =dt.addMonths(i);
arrobj.Name = oppobj.name;
arrobj.oppAmount__c = oppobj.Amount;
List<OpportunityLineItem> oppli= [select Id from OpportunityLineItem
where OpportunityId = : oppobj.Id];
arrobj.Opportunity_Product__c = oppli[0].id;
arrobj.OpportunityArr__c= oppobj.Id;
ArroppList.add(arrobj);
}
insert arropplist;
Test.startTest();
//TriggerHandler_opportunity closedTrigger=new TriggerHandler_opportunity();
//TriggerHandler_opportunity.CreateRecord(setopportunityId);
// TriggerHandler_opportunity.updateopprecord(setopportunityId);
PreventOppArrRecurssion.firstcall =false;
opportunityArrTriggerHandler.UpdateArrOPP(trigger.new,trigger.oldmap);
Test.stopTest();
}
}
I have highlited the changes only make those changes to the code. As I don't have that custom object so commented the code for that.
As said earlier I hope no need to call the method at all as the trigger will execute when a record is created or edited.
Thanks,
All Answers
For a Trigger or Handler no need to call the method from the test class because the trigger will execute once the record is created with out even calling the method.
Also test class don't know the Trigger.new and Trigger.oldmap. You need to pass the List of old opportuniues and map of new opportuites to the method as below.
If this solution helps, Please mark it as best answer.
Thanks,
I have highlited the changes only make those changes to the code. As I don't have that custom object so commented the code for that.
As said earlier I hope no need to call the method at all as the trigger will execute when a record is created or edited.
Thanks,
Yes that should be fine I guess.
Please mark my answer as best answer if it helped you.
Thanks,