function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeff JobsJeff Jobs 

Need Help with a Test Class

Hi all,

 

Still new to Apex and am having trouble with this test class:

 

@isTest

public class emailHandlerTest {

static testMethod void emailTest() {

//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 opp = new Opportunity();
opp.AccountId = a.Id;
opp.Name = 'test';
opp.StageName = 'test';
opp.CloseDate = system.today();
opp.RecordTypeId = rt.Id;
opp.Property__c = prop.Id;
insert opp;

rt = [SELECT Id FROM RecordType WHERE SobjectType = 'Applicants__c' AND isActive = true AND Name like '%Primary_Applicant%' LIMIT 1];
Applicants__c app = new Applicants__c();
app.RecordTypeId = rt.Id;
app.Opportunity_Name__c = opp.Id;
app.Property__c = prop.Id;
app.Applicant_Email__c = 'test@test.com';

//Initiate Test One - Will the first email field fill in after an applicant record is inserted?
Test.startTest();

insert app;

Test.stopTest();

system.assert(opp.Additional_Applicant_1__c == 'test@test.com'); //This is Line 43
}
}

 

Actual Trigger being tested is here:

 

trigger emailHandler on Applicants__c (after insert) {

Set<ID> opportunityIds = new Set<ID>();

for (Applicants__c a: trigger.new) {

opportunityIds.add(a.Opportunity_Name__c);
}

List<Applicants__c> applicantEmail = [Select id, Applicant_Email__c From Applicants__c Where id in: Trigger.new];

List<Opportunity> emailFields = [Select id, Additional_Applicant_1__c, Additional_Applicant_2__c,Additional_Applicant_3__c,Additional_Applicant_4__c
From Opportunity
Where id in: opportunityIds];

for (Opportunity o: emailFields) {

for (Applicants__c a: applicantEmail) {

String email = a.Applicant_Email__c;

if (o.Additional_Applicant_1__c == null) {

o.Additional_Applicant_1__c = email;

}Else if (o.Additional_Applicant_1__c <> null && o.Additional_Applicant_2__c == null) {

o.Additional_Applicant_2__c = email;

}Else if (o.Additional_Applicant_1__c <> null && o.Additional_Applicant_2__c <> null && o.Additional_Applicant_3__c == null) {

o.Additional_Applicant_3__c = email;

}Else if (o.Additional_Applicant_1__c <> null && o.Additional_Applicant_2__c <> null && o.Additional_Applicant_3__c <> null && o.Additional_Applicant_4__c == null) {

o.Additional_Applicant_4__c = email;
}
}
}

update emailFields;
}

 

As you can see I'm creating fake records so that I can fulfill the required fields on my Applicant__c object.  I have to have an account to create the property, a property to create the opportunity, etc.  

 

The trigger I'm testing is "after insert" so that's why I'm putting the "insert app" line in within the test; because I want to see what happens to the opportunity after an applicant record is inserted.  What should happen is that "test@test.com" gets put in an opportunity field called "Additional_Applicant_1__c".

 

I'm using the Developer Console and there are no errors to solve.  I run the test and get "Assertion Failed" at line 43, which I take to mean that "test@test.com" is NOT present on that field.

 

The trouble is that an actual test of this code works perfectly (meaning if I physically create the applicant record instead of using the test class).

 

Bottom Line: I know the code works, but I can't get the test class to prove it.  Please help!

 

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

 

You need to re-query the opportunity with Additional_Applicant_1__c field to resolve the test failure.

 

You can replace the following line

 

system.assert(opp.Additional_Applicant_1__c == 'test@test.com'); //This is Line 43

 

with

 

opp = [select Id, Name, AccountId, StageName, CloseDate, RecordTypeId, Property__c, Additional_Applicant_1__c from Opportunity where Id = :oppId];

 

system.assert(opp.Additional_Applicant_1__c == 'test@test.com'); //This is Line 43

 


p.s. Please leave your feedback (Kudos), if the suggested solution is working.

All Answers

TanyrilTanyril

Just curious if you've tried doing this through Eclipse or something and have any logs we can look at?

 

I don't think you need to do Test.startTest(); and Test.stopTest();

 

All of the test classes I've done for triggers essentially just create a test set of data and then insert it (if it's on insert) or do whatever would cause the trigger to fire

For instance:

 

@isTest
private class TestHistorical {

   public static testMethod void testBatch() {
    
    Account a = new Account(Name = 'Test Account', ShippingPostalCode = '00000', BillingCountry = 'US', navmfv2__AUM_as_of__c = Date.today());
    insert a;
    update a; 
    
	Historical_AUM__c b = new Historical_AUM__c( Account__c = a.Id, AUM__c = a.navmfv2__Latest_AUM__c, Value__c = a.Value__c, Research__c = a.Research__c, Record_Date__c = a.navmfv2__AUM_as_of__c, Balanced__c = a.Balanced__c, Government_MM__c = a.Government_MM__c, NE_Tax_Free__c = a.NE_Tax_Free__c, Hickory__c = a.Hickory__c, Partners_Value__c = a.Partners_Value__c, PIII__c = a.PIII__c, UpsertID__c = a.HAUMUpsertID__c);
	upsert b;
}
}

 is the test class for

 

trigger HistoricalAUM on Account (after insert, after update) {

List<Historical_AUM__c> TransferAUM = new List <Historical_AUM__c> {};

  Account[] accts;
    if (Trigger.isDelete) 
        accts = Trigger.old;
    else
        accts = Trigger.new;

for (account a : accts) {

TransferAUM.add( new Historical_AUM__c(Year_Revenue__c = a.Revenue__c, Revenue__c = a.Month_Revenue__c, Account__c = a.Id, AUM__c = a.navmfv2__Latest_AUM__c, Value__c = a.Value__c, Research__c = a.Research__c, Record_Date__c = a.navmfv2__AUM_as_of__c, Balanced__c = a.Balanced__c, Government_MM__c = a.Government_MM__c, NE_Tax_Free__c = a.NE_Tax_Free__c, Hickory__c = a.Hickory__c, Partners_Value__c = a.Partners_Value__c, PIII__c = a.PIII__c, PIII_Inv__c = a.PIII_Inv__c, Short_Int_Inst__c = a.Short__c, Short_Int_Inv__c = a.Short_Intermediate_Inv__c, UpsertID__c = a.HAUMUpsertID__c,Inflows__c = a.inflows__c, Net_Flows__c = a.Net_Flows__c, Outflows__c = a.Outflows__c, Month_Inflow__c = a.Month_In__c, Month_Net_Flows__c = a.Month_Net_Flows__c, Month_Outflow__c = a.Month_Out__c,Balanced_Net_Flows__c = a.Balanced_Month_Net_flow__c, Government_Net_Flows__c=a.Government_Monthly_Net_flow__c, Hickory_Net_Flows__c = a.Hickory_Month_Net_flows__c, NETF_Net_Flows__c = a.NETF_Monthly_Net__c, PIII_Inst_Net_Flows__c = a.PIII_Inst_Month_Net__c, PIII_Inv_Net_Flows__c = a.PIII_Inv_Month_Net__c, Pval_Net_Flows__c = a.PVAL_Monthly_Net__c, Research_Net_Flows__c = a.Research_Month_Net__c, Short_Inst_Net_Flows__c = a.Short_Inst_Month_Net__c, Short_Inv_Net_Flows__c = a.Short_Inv_Month_Net__c, Value_Net_Flows__c = a.Value_Month_Net_flow__c));
 
}
try {
upsert TransferAUM UpsertID__c;
}
catch (Exception Ex)
{
system.debug(Ex);
}
}

 

Kiran  KurellaKiran Kurella

 

You need to re-query the opportunity with Additional_Applicant_1__c field to resolve the test failure.

 

You can replace the following line

 

system.assert(opp.Additional_Applicant_1__c == 'test@test.com'); //This is Line 43

 

with

 

opp = [select Id, Name, AccountId, StageName, CloseDate, RecordTypeId, Property__c, Additional_Applicant_1__c from Opportunity where Id = :oppId];

 

system.assert(opp.Additional_Applicant_1__c == 'test@test.com'); //This is Line 43

 


p.s. Please leave your feedback (Kudos), if the suggested solution is working.

This was selected as the best answer
Jeff JobsJeff Jobs

Thanks so much CodeWizard!!  This solved it.  However, I did have to change your code by adding a "." between opp and Id.  So:

 

opp = [select Id, Name, AccountId, StageName, CloseDate, RecordTypeId, Property__c, Additional_Applicant_1__c from Opportunity where Id = :opp.Id];