You need to sign in to do that
Don't have an account?
Sanu Verma
Not able to update Opportunity field Test Class
the problem is i am not able to update opportunity field meanss their is class which have some condtion. i write a test class to cover that class.
and here is my test class
When i debug oldOpp and newOpp both return same value mnss it cannot get newer one can someone help me why is it so..
public with sharing class Sample{ public static void lastThreeMonthsVolumeOnLead(List<Opportunity> opps, Map<Id, Opportunity> oldOpps){ Map<String, BankVolumeInfoOnLeadWrapper> appIdWithBankInformation = new Map<String, BankVolumeInfoOnLeadWrapper>(); System.debug(opps); System.debug(oldOpps); for(Opportunity opp : opps){ //this condition which i want to make fullfill in test class if(opp.Volume_Last_Month__c != oldOpps.get(opp.Id).Volume_Last_Month__c || opp.Volume_Two_Months_Ago__c != oldOpps.get(opp.Id).Volume_Two_Months_Ago__c || opp.Volume_Three_Months_Ago__c != oldOpps.get(opp.Id).Volume_Three_Months_Ago__c ){ System.debug('hello i m here'); BankVolumeInfoOnLeadWrapper bankVolumeWrapper = new BankVolumeInfoOnLeadWrapper(); bankVolumeWrapper.volumeLastMonth = opp.Volume_Last_Month__c; bankVolumeWrapper.volumeTwoMonthsAgo = opp.Volume_Two_Months_Ago__c; bankVolumeWrapper.volumeThreeMonthsAgo = opp.Volume_Three_Months_Ago__c; appIdWithBankInformation.put(opp.Id, bankVolumeWrapper); } } if(appIdWithBankInformation.size() != 0) updateLeadWithBankVolumes(appIdWithBankInformation); } static void updateLeadWithBankVolumes(Map<String, BankVolumeInfoOnLeadWrapper> appIdWithBankInformation){ Map<Id, Lead> leadsToUpdate = new Map<Id, Lead>(); for(Lead lead : [SELECT Id, Volume_Last_Month__c, Volume_Two_Months_Ago__c, Volume_Three_Months_Ago__c, Renewal_App__c FROM Lead WHERE IsConverted = false AND Renewal_App__c != null AND Renewal_App__c IN: appIdWithBankInformation.keySet()]){ BankVolumeInfoOnLeadWrapper bankVolumeWrapper = appIdWithBankInformation.get(lead.Renewal_App__c); lead.Volume_Last_Month__c = bankVolumeWrapper.volumeLastMonth; lead.Volume_Two_Months_Ago__c = bankVolumeWrapper.volumeTwoMonthsAgo; lead.Volume_Three_Months_Ago__c = bankVolumeWrapper.volumeThreeMonthsAgo; leadsToUpdate.put(lead.Id, lead); } if(leadsToUpdate.size() != 0) update leadsToUpdate.values(); } public class BankVolumeInfoOnLeadWrapper{ public Decimal volumeLastMonth = 0; public Decimal volumeTwoMonthsAgo = 0; public Decimal volumeThreeMonthsAgo = 0; public bankVolumeInfoOnLeadWrapper(){} } }In above code i made bold tht condition which i want to full fill in test class.
and here is my test class
@isTest public class SampleTest{ @isTest static void getlastThreeMonthsVolumeOnLead() { Test.startTest(); insert UtilityTest.getTriggerExecutionCS(); Account acc = UtilityTest.getAccountWithRecordLabel('Broker Account'); insert acc; Account acc1=UtilityTest.getAccountWithRecordLabel('Merchant Accounts'); acc1.Name= 'Tiger Funding'; insert acc1; Opportunity opp=UtilityTest.getOpportunity(acc.Id); opp.Volume_Last_Month__c=5; opp.Volume_Two_Months_Ago__c=25; opp.Volume_Three_Months_Ago__c =20; opp.Name='Test1'; insert opp; System.debug(opp); opp.Volume_Last_Month__c=60; opp.Volume_Two_Months_Ago__c=50; opp.Volume_Three_Months_Ago__c =40; opp.Name='Test12'; update opp; System.debug(opp); Test.stopTest(); } }
When i debug oldOpp and newOpp both return same value mnss it cannot get newer one can someone help me why is it so..
Pleas use the below Test Class to get the 100% code coverage;
@isTest
public class UpdaterelatedLead_test{
@testSetup
static void setup() {
Account acc = new Account(Name = 'Test Acc');
insert acc;
Opportunity testOpportunity = new Opportunity(
Name = 'Test Opportunity',
Volume_Last_Month__c=5,
Volume_Two_Months_Ago__c=25,
Volume_Three_Months_Ago__c =20,
AccountId = acc.id,
StageName = 'Prospecting',
CloseDate = system.Today()
);
insert testOpportunity;
Lead ld = new lead(
LastName = 'Test Lead',
Renewal_App__c = testOpportunity.id,
Company = 'test Company',
Status = 'Open - Not Contacted'
);
insert ld;
}
@isTest
static void testMethod1() {
Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
opp.Volume_Last_Month__c=60;
System.test.startTest();
update opp;
System.test.stopTest();
}
@isTest
static void testMethod2() {
Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
opp.Volume_Two_Months_Ago__c=50;
System.test.startTest();
update opp;
System.test.stopTest();
}
@isTest
static void testMethod3() {
Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
opp.Volume_Three_Months_Ago__c =40;
System.test.startTest();
update opp;
System.test.stopTest();
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
All Answers
Mark as best answer, If it resloves !!
I have update your code now oppournity will update and you need to add functionlaity as per your logic Mark as resloved if it helps :) :)
Thanks,
Bhanu Prakash
visit ForceLearn.com (https://www.forcelearn.com)
Pleas use the below Test Class to get the 100% code coverage;
@isTest
public class UpdaterelatedLead_test{
@testSetup
static void setup() {
Account acc = new Account(Name = 'Test Acc');
insert acc;
Opportunity testOpportunity = new Opportunity(
Name = 'Test Opportunity',
Volume_Last_Month__c=5,
Volume_Two_Months_Ago__c=25,
Volume_Three_Months_Ago__c =20,
AccountId = acc.id,
StageName = 'Prospecting',
CloseDate = system.Today()
);
insert testOpportunity;
Lead ld = new lead(
LastName = 'Test Lead',
Renewal_App__c = testOpportunity.id,
Company = 'test Company',
Status = 'Open - Not Contacted'
);
insert ld;
}
@isTest
static void testMethod1() {
Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
opp.Volume_Last_Month__c=60;
System.test.startTest();
update opp;
System.test.stopTest();
}
@isTest
static void testMethod2() {
Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
opp.Volume_Two_Months_Ago__c=50;
System.test.startTest();
update opp;
System.test.stopTest();
}
@isTest
static void testMethod3() {
Opportunity opp = [SELECT Id, Name, AccountId, CloseDate, StageName, Volume_Last_Month__c, Volume_Two_Months_Ago__c,Volume_Three_Months_Ago__c FROM Opportunity LIMIT 1];
opp.Volume_Three_Months_Ago__c =40;
System.test.startTest();
update opp;
System.test.stopTest();
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj