You need to sign in to do that
Don't have an account?
SalesforceCrm AccountCRM
System.AssertException: Assertion Failed error in test class
When i run a test class the system throws an error as
Trigger :
if i uncomment the line ,the system throws an error : System.AssertException: Assertion Failed System.assert(mc1res.New_to_EnterpriseDB__c);If comment the above line ,then the method gets passed .
Trigger :
public class CampaignMemberFunctions { // Sets the CampaignMember New_to_Enterprise_Db field if lead was created in lsat 6 hours // Call during CampaignMember before create trigger public static void UpdateNewToEnterpriseDB(CampaignMember cm, Lead ld) { if(ld.CreatedDate.addHours(6) > DateTime.Now()) cm.New_to_EnterpriseDB__c = true; } // Updates the lead or contact status based on sync logic. Returns True if update occurred // ld or ct must be valid. If ct is valid, ct.Account.Customer_Status__c must be valid. // Note for developers - yes, it's not the most efficient possible code, but is clear, and leaves flexibility // for more complex mappings later. public static Boolean SyncStatus(CampaignMember cm, Lead ld, Contact ct, Map<String, CMStatusSettings__c> statusmap) { Boolean result = true; String newvalue; if(ct!= null) { if(statusmap.containsKey(cm.Response_Status__c)) { newvalue = statusmap.get(cm.Response_Status__c).StatusMapping__c; if(newvalue == ct.Status__c) { result = false; } else { ct.Status__c = newvalue; } } else if(cm.Response_Status__c == 'Disqualified' || cm.Response_Status__c == 'Qualified - New Opportunity' || cm.Response_Status__c == 'Closed - Converted' ) { newvalue = 'Closed - ' + ct.Account.Customer_Status__c + ' Account'; if(ct.Status__c == newvalue) { result = false; } else { ct.Status__c = newvalue; } if(cm.Response_Status__c == 'Disqualified') { // Set nuture time on disqualified status Date newtimeout = Date.today().addDays((CMScoreEngine__c.getInstance('Default').NurtureTimeoutDays__c).intValue()); if(ct.Admin_Nurture_Timeout__c != newtimeout || ct.Lead_Score__c !=0 || ct.Campaign_Score__c !=0) result = true; ct.Admin_Nurture_Timeout__c = newtimeout; ct.Lead_Score__c = 0; ct.Campaign_Score__c = 0; // Probably aren't using this one } } else { result = false; } /*if(cm.Response_Status__c=='Contacted-Warm' && cm.Do_not_reassign_to_Landings__c) { ct.Do_not_reassign_to_Landings__c = true; result = true; }*/ } else if(ld!=null) { if(statusmap.containsKey(cm.Response_Status__c)) { newvalue = statusmap.get(cm.Response_Status__c).StatusMapping__c; if(newvalue==ld.Status) { result = false; } else { ld.Status = newvalue; } } else if(cm.Response_Status__c == 'Disqualified') { if(ld.Status=='Disqualified' && ld.Disqaulified_Reason__c == cm.Disqualified_Reason__c) { result = false; } else { ld.Status = 'Disqualified'; ld.Disqaulified_Reason__c = cm.Disqualified_Reason__c; System.Debug('Disqualified reason being set to: ' + cm.Disqualified_Reason__c); } } else { result = false; } /*if(cm.Response_Status__c=='Contacted-Warm' && cm.Do_not_reassign_to_Landings__c) { ld.Do_not_reassign_to_Landings__c = true; result = true; }*/ } return(result); } // Search through the converted leads, opportunities and campaignmembers to sync if necessary public static void SyncAfterConversion(Map<Id,Lead> convertedleads, Map<Id, Opportunity>convertedops, Map<Id, CampaignMember> possiblecms) { for(Lead ld: convertedleads.values()) { // Grab the campaign member - this must be correct by original query CampaignMember cm = new CampaignMember(); System.debug('@@@@@possiblecms.get(ld.admin_CMSourceId__c)'+possiblecms.get(ld.admin_CMSourceId__c)); cm = possiblecms.get(ld.admin_CMSourceId__c); System.debug('@@@@@cm '+cm ); if(cm!=null) { // We know this lead is converted. Let's get the opportunity. if(ld.ConvertedOpportunityId != null) { Opportunity opp = convertedops.get(ld.ConvertedOpportunityId); opp.PMM_stage__c = cm.PMM_Stage__c; opp.Product_Interest__c = cm.Product_Interest__c; opp.Buyer__c = cm.Buyer__c; opp.New_to_EnterpriseDB__c = cm.New_to_EnterpriseDB__c; opp.Problem_to_Solve__c = cm.Problem_to_Solve__c; opp.Any_OSS_in_current_stack__c = cm.Any_OSS_in_current_stack__c; opp.Total_of_Databases_in_your_Org__c = cm.Total_of_Databases_in_your_Org__c; opp.Primary_Database__c = cm.Primary_Database__c; opp.of_Postgres_Database_Servers__c = cm.of_Postgres_Database_Servers__c; opp.Just_learning_about_EnterpriseDB__c = cm.Just_learning_about_EnterpriseDB__c; opp.Project_Requires_a_Net_New_DB__c = cm.Project_Requires_a_Net_New_DB__c; opp.Timeframe_To_Production__c = cm.Timeframe_To_Production__c; opp.Budget__c = cm.Budget__c; opp.User__c = cm.User__c; opp.Champion__c = cm.Champion__c; opp.Decision_Maker__c = cm.Decision_Maker__c; cm.Response_Status__c = 'Qualified - New Opportunity'; cm.Opportunity_Name__c = opp.Id; cm.Opportunity_Create_Date__c = Date.Today(); } else { if(cm.lead.status!=null) { cm.Response_Status__c = cm.lead.status; } } cm.admin_IsConverted__c = true; } } } public static void EmailErrorMessage(String message) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[] {'edbnotify@bearcavern.com', 'roanbear@gmail.com'}); mail.setSenderDisplayName('APEX notifier'); mail.setSubject('SFDC/Eloqua Data Creation Error'); mail.setPlainTextBody(message); List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); for(Messaging.SendEmailResult r: results) { System.debug('Result of Email sent ' + r.isSuccess()); if(!r.isSuccess()) { for(Messaging.SendEmailError sme: r.getErrors()) { for(String s: sme.getFields()) System.Debug('Error for field ' + s ); System.Debug('Error message ' + sme.getMessage()); System.Debug('StatusCode ' + sme.getStatusCode()); } } } } }Part of Test Class Written :
@isTest Public class TestCampaignMemberFunctions { //Checking for if Condition value Response_Status__c= Disqualified Public static testMethod void TestCreationDate() { Test.StartTest(); Lead ld2 = [select ID, CreatedDate, IsConverted from LEAD where CreatedDate < :DateTime.Now().addHours(-7) and IsConverted=False LIMIT 1]; Campaign cam = new Campaign(Name='campname'); insert cam; Account a = new Account(); a.Name ='My Company'; Insert a; Lead ld = new Lead(); ld.Company='colead1'; ld.LastName ='colead1_'+String.valueOf(System.now()); ld.status= 'Disqualified'; ld.admin_CMSourceId__c= cam.Id; ld.Disqaulified_Reason__c='Disqualified'; insert ld; Contact con = new Contact(LastName ='Test'); Insert con; // Test.StartTest(); CampaignMember mc1 = new CampaignMember(CampaignId = cam.Id,Product_Interest__c='Services',Response_Status__c='Disqualified',status= 'Disqualified',PMM_Stage__c='Learns of Postgres', LeadId=ld.Id); CampaignMember mc2 = new CampaignMember(CampaignId = cam.Id, LeadId=ld2.Id); insert mc1; insert mc2; Opportunity opp =new Opportunity(); opp.Product_Interest__c='Services'; opp.PMM_stage__c ='Deployment'; opp.Buyer__c = 'xyz'; opp.New_to_EnterpriseDB__c =True; opp.Problem_to_Solve__c ='Performance' ; opp.Any_OSS_in_current_stack__c ='OS' ; opp.Total_of_Databases_in_your_Org__c ='500'; opp.Primary_Database__c =mc1.Primary_Database__c; opp.of_Postgres_Database_Servers__c = mc1.of_Postgres_Database_Servers__c; opp.Just_learning_about_EnterpriseDB__c = mc1.Just_learning_about_EnterpriseDB__c; opp.Project_Requires_a_Net_New_DB__c = mc1.Project_Requires_a_Net_New_DB__c; opp.Timeframe_To_Production__c = mc1.Timeframe_To_Production__c; opp.Budget__c = mc1.Budget__c; opp.User__c = mc1.User__c; opp.Champion__c = mc1.Champion__c; opp.Decision_Maker__c =mc1.Decision_Maker__c; opp.Name='Testing'; opp.StageName='Closed Lost'; opp.CloseDate=System.today(); Insert opp; CampaignMember mc1res = [Select Id, New_to_EnterpriseDB__c,Response_Status__c from CampaignMember where Id = :mc1.id ]; CampaignMember mc2res = [Select Id, New_to_EnterpriseDB__c from CampaignMember where Id = :mc2.id ]; System.assert(mc1res.New_to_EnterpriseDB__c); System.assert(! mc2res.New_to_EnterpriseDB__c); Map<String, CMStatusSettings__c> statusmap = new Map<String, CMStatusSettings__c>(); // statusmap.put(mc1res.Response_Status__c, custSetting); Map<Id,Lead> convertedleads = new Map<Id,Lead>(); convertedleads.put(ld.Id,ld); Map<Id, Opportunity> convertedops= new Map<Id, Opportunity>(); convertedops.put(opp.Id,opp); Map<Id, CampaignMember> possiblecms= new Map<Id, CampaignMember>(); possiblecms.put(mc1.Id,mc1); CampaignMemberFunctions cmf = new CampaignMemberFunctions(); CampaignMemberFunctions.SyncStatus( mc1res,ld,con,statusmap); System.debug('@@@@@convertedleads'+convertedleads); System.debug('@@@@@possiblecms'+possiblecms); System.debug('@@@@@convertedops'+convertedops); CampaignMemberFunctions.SyncAfterConversion(convertedleads,convertedops,possiblecms); CampaignMemberFunctions.EmailErrorMessage('testmessage'); Test.StopTest(); } }Any help very much appreciated.
It doesn't look like your method "UpdateNewToEnterpriseDB" is being called anywhere. In the test class, you are asserting that the checkbox was checked, but if the method is never called, then it would not be checked.
If you remove assert statements, the only way a test class fails is if it runs into a system exception or validation rule. Just becuase a test passes does not mean that the code is working as expected. That is why assert statements are important to ensure the code is functioning properly.
Hope that helps
As Suggested i tried adding the method ,but the system throws an error as : Any help very much appreciated.
THe varibles are defined in the trigger
public static void UpdateNewToEnterpriseDB(CampaignMember cm, Lead ld)
When i use
CampaignMemberFunctions.UpdateNewToEnterpriseDB(cm,ld)
then the system throws an error as Similarly when i use the other variable used in test class as Run test class ,Throws an error as : Any suggestion plz.
"Variable does not exist: cm" error means that you are trying to use "cm" and is it not defined as a variable.
Please post the code where you are trying to call the method and I will give some more specific guidance. The original post included only the helper and test class, not the trigger.
Trigger :
Test Class : Any help very much appreciated.
If you do not have a trigger defined, then that is your issue.
before calling line 56 and 57:
Since you are checking the functionality of UpdateNewToEnterpriseDB by asserting, you need to call the method first. Then assert what should happen.