+ Start a Discussion
Jeff JobsJeff Jobs 

Help with Test Class Where an Update is Required

Hi,

 

I'm writing a test class where I'm testing a "before update" scenario.  

 

Original Class is:

 

public class paymentRecordHandlerRentIn{

//Create a method for GTM Deals
public static void rentIn (Payment_Record__c[] paymentRecords) {

for (Payment_Record__c pr: paymentRecords) {

//Declare Variables
Date month1Date = pr.Move_In_Date__c;
Date month2Date = (((pr.Move_In_Date__c.toStartOfMonth()).addMonths(1)).addDays(24));
Date month3Date = month2Date.addMonths(1);
Date month4Date = month3Date.addMonths(1);
Date month5Date = month4Date.addMonths(1);
Date month6Date = month5Date.addMonths(1);
Date month7Date = month6Date.addMonths(1);
Date month8Date = month7Date.addMonths(1);
Date month9Date = month8Date.addMonths(1);
Date month10Date = month9Date.addMonths(1);
Date month11Date = month10Date.addMonths(1);
Date month12Date = month11Date.addMonths(1);
Date month13Date = month12Date.addMonths(1);
Date month14Date = month13Date.addMonths(1);
Date month15Date = month14Date.addMonths(1);
Date month16Date = month15Date.addMonths(1);
Date month17Date = month16Date.addMonths(1);
Date month18Date = month17Date.addMonths(1);
Date month19Date = month18Date.addMonths(1);
Date month20Date = month19Date.addMonths(1);
Date month21Date = month20Date.addMonths(1);
Date month22Date = month21Date.addMonths(1);
Date month23Date = month22Date.addMonths(1);
Date month24Date = month23Date.addMonths(1);
Date month25Date = month24Date.addMonths(1);
Date month26Date = month25Date.addMonths(1);
Date month27Date = month26Date.addMonths(1);
Date month28Date = month27Date.addMonths(1);
Date month29Date = month28Date.addMonths(1);
Date month30Date = month29Date.addMonths(1);
Date month31Date = month30Date.addMonths(1);
Date month32Date = month31Date.addMonths(1);
Date month33Date = month32Date.addMonths(1);
Date month34Date = month33Date.addMonths(1);
Date month35Date = month34Date.addMonths(1);
Date month36Date = month35Date.addMonths(1);
Date month37Date = month36Date.addMonths(1);
Date month38Date = month37Date.addMonths(1);
Date month39Date = month38Date.addMonths(1);
Date month40Date = month39Date.addMonths(1);
Date month41Date = month40Date.addMonths(1);
Date month42Date = month41Date.addMonths(1);
Date month43Date = month42Date.addMonths(1);
Date month44Date = month43Date.addMonths(1);
Date month45Date = month44Date.addMonths(1);
Date month46Date = month45Date.addMonths(1);
Date month47Date = month46Date.addMonths(1);
Date month48Date = month47Date.addMonths(1);
Date month49Date = month48Date.addMonths(1);
Date month50Date = month49Date.addMonths(1);
Date month51Date = month50Date.addMonths(1);
Date month52Date = month51Date.addMonths(1);
Date month53Date = month52Date.addMonths(1);
Date month54Date = month53Date.addMonths(1);
Date month55Date = month54Date.addMonths(1);
Date month56Date = month55Date.addMonths(1);
Date month57Date = month56Date.addMonths(1);
Date month58Date = month57Date.addMonths(1);
Date month59Date = month58Date.addMonths(1);
Date month60Date = month59Date.addMonths(1);
Date finalPaymentDate = month60Date.addMonths(1);

Decimal fPRP = pr.Prorated_Rent_Amount__c;
Decimal rFR1 = pr.Rent_from_Resident_Year_One__c;
Decimal rFR2 = pr.Rent_from_Resident_Year_Two__c;
Decimal rFR3 = pr.Rent_from_Resident_Year_Three__c;
Decimal rFR4 = pr.Rent_from_Resident_Year_Four__c;
Decimal rFR5 = pr.Rent_from_Resident_Year_Five__c;

//Standard Rent Agreement
if(pr.RecordTypeId == '012J00000000MgB') {

//Print "No Matter What" fields
pr.Month_1_Payment_Date__c = month1Date;
pr.Month_2_Payment_Amount__c = rFR1;
pr.Month_2_Payment_Date__c = month2Date;
pr.Month_3_Payment_Amount__c = rFR1;
pr.Month_3_Payment_Date__c = month3Date;
pr.Month_4_Payment_Amount__c = rFR1;
pr.Month_4_Payment_Date__c = month4Date;
pr.Month_5_Payment_Amount__c = rFR1;
pr.Month_5_Payment_Date__c = month5Date;
pr.Month_6_Payment_Amount__c = rFR1;
pr.Month_6_Payment_Date__c = month6Date;
pr.Month_7_Payment_Amount__c = rFR1;
pr.Month_7_Payment_Date__c = month7Date;
pr.Month_8_Payment_Amount__c = rFR1;
pr.Month_8_Payment_Date__c = month8Date;
pr.Month_9_Payment_Amount__c = rFR1;
pr.Month_9_Payment_Date__c = month9Date;
pr.Month_10_Payment_Amount__c = rFR1;
pr.Month_10_Payment_Date__c = month10Date;
pr.Month_11_Payment_Amount__c = rFR1;
pr.Month_11_Payment_Date__c = month11Date;
pr.Month_12_Payment_Amount__c = rFR1;
pr.Month_12_Payment_Date__c = month12Date;
pr.Stop_Code__c = TRUE;

//Prorated Rent
if (pr.Override_Prorated_Rent__c == FALSE) {

pr.Month_1_Payment_Type__c = 'First Prorated Rent Amount';
pr.Month_1_Payment_Amount__c = fPRP;
pr.Final_Payment_Amount__c = rFR1;
pr.Final_Payment_Date__c = finalPaymentDate;

//No Prorated Rent
}Else if (pr.Override_Prorated_Rent__c == TRUE) {

pr.Month_1_Payment_Type__c = 'Standard Rent Payment';
pr.Month_1_Payment_Amount__c = rFR1;
}

//Begin Multi-Year Logic

//Year One
}Else if(pr.RecordTypeId == '012J00000000LyZ') {

//Print "No Matter What" fields
pr.Month_1_Payment_Date__c = month1Date;
pr.Month_2_Payment_Amount__c = rFR1;
pr.Month_2_Payment_Date__c = month2Date;
pr.Month_3_Payment_Amount__c = rFR1;
pr.Month_3_Payment_Date__c = month3Date;
pr.Month_4_Payment_Amount__c = rFR1;
pr.Month_4_Payment_Date__c = month4Date;
pr.Month_5_Payment_Amount__c = rFR1;
pr.Month_5_Payment_Date__c = month5Date;
pr.Month_6_Payment_Amount__c = rFR1;
pr.Month_6_Payment_Date__c = month6Date;
pr.Month_7_Payment_Amount__c = rFR1;
pr.Month_7_Payment_Date__c = month7Date;
pr.Month_8_Payment_Amount__c = rFR1;
pr.Month_8_Payment_Date__c = month8Date;
pr.Month_9_Payment_Amount__c = rFR1;
pr.Month_9_Payment_Date__c = month9Date;
pr.Month_10_Payment_Amount__c = rFR1;
pr.Month_10_Payment_Date__c = month10Date;
pr.Month_11_Payment_Amount__c = rFR1;
pr.Month_11_Payment_Date__c = month11Date;
pr.Month_12_Payment_Amount__c = rFR1;
pr.Month_12_Payment_Date__c = month12Date;
pr.Stop_Code__c = TRUE;

//Prorated Rent
if (pr.Override_Prorated_Rent__c == FALSE) {

pr.Month_1_Payment_Type__c = 'First Prorated Rent Amount';
pr.Month_1_Payment_Amount__c = fPRP;

//No Prorated Rent
}Else if (pr.Override_Prorated_Rent__c == TRUE) {

pr.Month_1_Payment_Type__c = 'Standard Rent Payment';
pr.Month_1_Payment_Amount__c = rFR1;
}

//Year Two
}Else if (pr.RecordTypeId == '012J00000000MgG') {

//Print Variables
pr.Month_1_Payment_Amount__c = rFR2;
pr.Month_1_Payment_Date__c = month13Date;
pr.Month_2_Payment_Amount__c = rFR2;
pr.Month_2_Payment_Date__c = month14Date;
pr.Month_3_Payment_Amount__c = rFR2;
pr.Month_3_Payment_Date__c = month15Date;
pr.Month_4_Payment_Amount__c = rFR2;
pr.Month_4_Payment_Date__c = month16Date;
pr.Month_5_Payment_Amount__c = rFR2;
pr.Month_5_Payment_Date__c = month17Date;
pr.Month_6_Payment_Amount__c = rFR2;
pr.Month_6_Payment_Date__c = month18Date;
pr.Month_7_Payment_Amount__c = rFR2;
pr.Month_7_Payment_Date__c = month19Date;
pr.Month_8_Payment_Amount__c = rFR2;
pr.Month_8_Payment_Date__c = month20Date;
pr.Month_9_Payment_Amount__c = rFR2;
pr.Month_9_Payment_Date__c = month21Date;
pr.Month_10_Payment_Amount__c = rFR2;
pr.Month_10_Payment_Date__c = month22Date;
pr.Month_11_Payment_Amount__c = rFR2;
pr.Month_11_Payment_Date__c = month23Date;
pr.Month_12_Payment_Amount__c = rFR2;
pr.Month_12_Payment_Date__c = month24Date;
pr.Stop_Code__c = TRUE;

//Year Three
}Else if (pr.RecordTypeId == '012J00000000MgL') {

//Print Variables
pr.Month_1_Payment_Amount__c = rFR3;
pr.Month_1_Payment_Date__c = month25Date;
pr.Month_2_Payment_Amount__c = rFR3;
pr.Month_2_Payment_Date__c = month26Date;
pr.Month_3_Payment_Amount__c = rFR3;
pr.Month_3_Payment_Date__c = month27Date;
pr.Month_4_Payment_Amount__c = rFR3;
pr.Month_4_Payment_Date__c = month28Date;
pr.Month_5_Payment_Amount__c = rFR3;
pr.Month_5_Payment_Date__c = month29Date;
pr.Month_6_Payment_Amount__c = rFR3;
pr.Month_6_Payment_Date__c = month30Date;
pr.Month_7_Payment_Amount__c = rFR3;
pr.Month_7_Payment_Date__c = month31Date;
pr.Month_8_Payment_Amount__c = rFR3;
pr.Month_8_Payment_Date__c = month32Date;
pr.Month_9_Payment_Amount__c = rFR3;
pr.Month_9_Payment_Date__c = month33Date;
pr.Month_10_Payment_Amount__c = rFR3;
pr.Month_10_Payment_Date__c = month34Date;
pr.Month_11_Payment_Amount__c = rFR3;
pr.Month_11_Payment_Date__c = month35Date;
pr.Month_12_Payment_Amount__c = rFR3;
pr.Month_12_Payment_Date__c = month36Date;
pr.Stop_Code__c = TRUE;

//Year Four
}Else if (pr.RecordTypeId == '012J00000000MgQ') {

//Print Variables
pr.Month_1_Payment_Amount__c = rFR4;
pr.Month_1_Payment_Date__c = month37Date;
pr.Month_2_Payment_Amount__c = rFR4;
pr.Month_2_Payment_Date__c = month38Date;
pr.Month_3_Payment_Amount__c = rFR4;
pr.Month_3_Payment_Date__c = month39Date;
pr.Month_4_Payment_Amount__c = rFR4;
pr.Month_4_Payment_Date__c = month40Date;
pr.Month_5_Payment_Amount__c = rFR4;
pr.Month_5_Payment_Date__c = month41Date;
pr.Month_6_Payment_Amount__c = rFR4;
pr.Month_6_Payment_Date__c = month42Date;
pr.Month_7_Payment_Amount__c = rFR4;
pr.Month_7_Payment_Date__c = month43Date;
pr.Month_8_Payment_Amount__c = rFR4;
pr.Month_8_Payment_Date__c = month44Date;
pr.Month_9_Payment_Amount__c = rFR4;
pr.Month_9_Payment_Date__c = month45Date;
pr.Month_10_Payment_Amount__c = rFR4;
pr.Month_10_Payment_Date__c = month46Date;
pr.Month_11_Payment_Amount__c = rFR4;
pr.Month_11_Payment_Date__c = month47Date;
pr.Month_12_Payment_Amount__c = rFR4;
pr.Month_12_Payment_Date__c = month48Date;
pr.Stop_Code__c = TRUE;

//Year Five
}Else if (pr.RecordTypeId == '012J00000000MgH') {

//Print Variables
pr.Month_1_Payment_Amount__c = rFR5;
pr.Month_1_Payment_Date__c = month49Date;
pr.Month_2_Payment_Amount__c = rFR5;
pr.Month_2_Payment_Date__c = month50Date;
pr.Month_3_Payment_Amount__c = rFR5;
pr.Month_3_Payment_Date__c = month51Date;
pr.Month_4_Payment_Amount__c = rFR5;
pr.Month_4_Payment_Date__c = month52Date;
pr.Month_5_Payment_Amount__c = rFR5;
pr.Month_5_Payment_Date__c = month53Date;
pr.Month_6_Payment_Amount__c = rFR5;
pr.Month_6_Payment_Date__c = month54Date;
pr.Month_7_Payment_Amount__c = rFR5;
pr.Month_7_Payment_Date__c = month55Date;
pr.Month_8_Payment_Amount__c = rFR5;
pr.Month_8_Payment_Date__c = month56Date;
pr.Month_9_Payment_Amount__c = rFR5;
pr.Month_9_Payment_Date__c = month57Date;
pr.Month_10_Payment_Amount__c = rFR5;
pr.Month_10_Payment_Date__c = month58Date;
pr.Month_11_Payment_Amount__c = rFR5;
pr.Month_11_Payment_Date__c = month59Date;
pr.Month_12_Payment_Amount__c = rFR5;
pr.Month_12_Payment_Date__c = month60Date;
pr.Stop_Code__c = TRUE;

//Prorated Rent
if (pr.Override_Prorated_Rent__c == FALSE) {

pr.Final_Payment_Amount__c = rFR5;
pr.Final_Payment_Date__c = finalPaymentDate;
}
}

}

}

}

 

Test Class is:

 

@isTest

 

public class paymentRecordsTestRentIn{

static testMethod void standardTest() {

//Create Fake Records
RecordType rt = [SELECT Id FROM RecordType WHERE SobjectType = 'Account' AND isActive = true AND DeveloperName like '%Person_Account%' LIMIT 1];
Account a = new Account();
a.RecordTypeId = rt.Id;
a.LastName = 'test';
insert a;

Property__c prop = new Property__c();
prop.Address_Line_1__c = '123 Street';
prop.Name = '123 Street';
insert prop;

rt = [SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' AND isActive = true AND Name like '%Rental%' LIMIT 1];
Opportunity oppR1 = new Opportunity();
oppR1.AccountId = a.Id;
oppR1.Name = 'test';
oppR1.StageName = 'test';
oppR1.CloseDate = system.today();
oppR1.RecordTypeId = rt.Id;
oppR1.Property__c = prop.Id;
oppR1.Move_In_Date__c = date.parse('05/17/2013');
oppR1.Resident_Lease_Date__c = date.parse('05/17/2013');
oppR1.Resident_Lease_End_Date__c = date.parse('05/17/2014');
oppR1.Days_of_the_Month__c = 31;
oppR1.Rent_From_Resident__c = 1000;
insert oppR1;

rt = [SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' AND isActive = true AND Name like '%Rental%' LIMIT 1];
Opportunity oppR2 = new Opportunity();
oppR2.AccountId = a.Id;
oppR2.Name = 'test';
oppR2.StageName = 'test';
oppR2.CloseDate = system.today();
oppR2.RecordTypeId = rt.Id;
oppR2.Property__c = prop.Id;
oppR1.Move_In_Date__c = date.parse('05/17/2013');
oppR1.Resident_Lease_Date__c = date.parse('05/17/2013');
oppR1.Resident_Lease_End_Date__c = date.parse('05/17/2014');
oppR2.Days_of_the_Month__c = 31;
oppR2.Rent_From_Resident__c = 1000;
oppR2.Rent_From_Resident_Year_2__c = 1050;
oppR2.Rent_From_Resident_Year_3__c = 1100;
oppR2.Rent_From_Resident_Year4__c = 1150;
oppR2.Rent_From_Resident_Year_5__c = 1200;
insert oppR2;

rt = [SELECT Id FROM RecordType WHERE SobjectType = 'Payment_Record__c' AND isActive = true AND DeveloperName like '%Rent_In%' LIMIT 1];
Payment_Record__c prHS1 = new Payment_Record__c();
prHS1.RecordTypeId = rt.Id;
prHS1.Opportunity__c = oppR1.id;
prHS1.Property__c = prop.id;
insert prHS1;

 

//Begin Testing

Test.startTest();

prHS1.Override_Prorated_Rent__c = FALSE;
update prHS1;

prHS1 = [Select id, Month_1_Payment_Type__c, Month_1_Payment_Amount__c, Month_1_Payment_Date__c,
Month_2_Payment_Amount__c, Month_2_Payment_Date__c, Final_Payment_Amount__c, Final_Payment_Date__c, Move_In_Date__c,
Stop_Code__c, Override_Prorated_Rent__c
From Payment_Record__c Where Id = :prHS1.id];

//Standard Rent with Prorated Rent Test
system.assert(prHS1.Month_1_Payment_Type__c == 'First Prorated Rent Amount'); //Line 69
system.assert(prHS1.Month_1_Payment_Amount__c == 483.87);
system.assert(prHS1.Month_1_Payment_Date__c == prHS1.Move_In_Date__c);
system.assert(prHS1.Month_2_Payment_Amount__c == 1000);
system.assert(prHS1.Month_2_Payment_Date__c == (((prHS1.Move_In_Date__c.toStartOfMonth()).addMonths(1)).addDays(24)));
system.assert(prHS1.Stop_Code__c == TRUE);

Test.stopTest();
}

}

 

Error Message is:

 

Assertion Failed at Line 69

 

I've marked Line 69 in comment code

 

What's happening is that even though I'm typing "update prHS1" no update is taking place.  Thus, the field at line 69 is null and passes if I type "== null" instead of my string.  I should mention that the original class code works when put into practice, and that no errors are provided in the developer console.  Obviously, I'm missing a huge piece for "before update" test classes (this is my first one).  

 

Any help would be appreciated.  Under a tight deadline, so would appreciate any thoughts sooner rather than later.  Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Jeff JobsJeff Jobs

Upon further review, I discovered that I was defining an improprer record type and there was a critical flaw in my trigger (not listed).  Apologies for posting :)