You need to sign in to do that
Don't have an account?
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!
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
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:
is the test class for
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.
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];