1

I have a trigger on Account that inserts into Property after insert and I am have trouble with the test class. I'm not sure how to handle it since I am new to Apex.

Here is the trigger.

trigger CreatePropertRecord on Account (after Insert) {

for(Account a : trigger.new)

if( a.Number_of_Properties__c > 0 )

    {

    Property__c v = new Property__c (); 

    if( a.Property_Name__c <> Null )
        v.Name = a.Property_Name__c;
    Else
        v.Name = a.Name;

    v.Property__c = a.id ;
    v.Property_Code__c = a.Property_ID__c ;
    v.Bathrooms_Number__c= a.Number_Bathrooms__c;
    v.Bedrooms_Number__c= a.Bedrooms_Number__c;
    v.Beach_Front__c = a.Beach_Front__c ;
    v.Street__c = a.Street__c;
    v.City__c = a.City__c;
    v.State_Province__c = a.State_Province__c;
    v.Zip_Postal_Code__c = a.Zip_Postal_Code__c;
    v.Country__c = a.Country__c;
    v.Ski_In_Ski_Out__c = a.Ski_In_Ski_Out__c ;
    v.Resort__c = a.Resort__c;
    v.Property_Type__c = a.Property_Type__c;
    v.Number_of_Properties__c = a.Number_of_Properties__c;
    v.Ownership_Status__c = a.Ownership_Status__c ;
    v.Property_Owner_Status__c = a.Property_Owner_Status__c;
    v.Expected_Annual_GLR__c = a.Expected_Annual_GLR__c;
    insert v ;
    }}

Here is the test class that was created that is not working.

@isTest 
private class CreatePropertRecordTest {
static testMethod void validateCreatePropertRecord() {
   account a = new account(
   a.Property_Name__c='test name',
   a.Property_ID__c='test id',
   a.Number_Bathrooms__c='1B',
   a.Bedrooms_Number__c='1B',
   a.Beach_Front__c=TRUE,
   a.Street__c='test street',
   a.City__c='test city',
   a.State_Province__c='test state',
   a.Zip_Postal_Code__c='test zip',
   a.Country__c='test country',
   a.Ski_In_Ski_Out__c=TRUE,
   a.Resort__c='test resort',
   a.Property_Type__c='Cabin',
   a.Number_of_Properties__c=1,
   a.Ownership_Status__c='Already own',
   a.Property_Owner_Status__c='LLC',
   a.Expected_Annual_GLR__c='50000'
   );

   // Insert account
   insert a;

   // Retrieve the new account
   a = [SELECT description FROM account WHERE Id =:a.Id];

   // Test that the trigger correctly updated the description
   System.assertEquals(null, a.description);
}} 
  • 1
    Your trigger doesn't seem to be updating the description. What are you actually having trouble with? You have many of the right ideas - what it seems like you should be doing is querying for the Property records and asserting it's values. – Stephen Sep 28 '16 at 15:43
  • Are you not including the account name in the method to create it? That would cause the account insert to fail. – Dave Humm Sep 28 '16 at 15:44
  • @Stephen, the trouble I am having is a lack of Apex experience. :) This test class was handed to me from someone else and I am trying to learn as I go. I assumed I need to assert the values created in Property once the trigger has fired. I'm not sure how to do that. I understand the need for test classes, etc, but I'm not sure how to build it for this trigger. In the test class, do I have to insert a record into account forcing the trigger to fire then select from Property looking for the new records? – Mays Sep 28 '16 at 16:46
  • @Mays Yes, that is exactly correct. Since this is a trigger on inserting Accounts that will create a new Property - that is that you have to test. After you insert the Account, instead of querying the account again, query the Property__c table SELECT fields WHERE Property__c = a.id and Assert that the fields from the Property query are the same as the Account that you inserted. – Stephen Sep 28 '16 at 16:54
2

The problems with your test class are:

  1. Account name is not passed to while creation of account
  2. Assert might fail because when you compare with text it will blank(eg: '' or null)

Best practice to follow for DML/soql:

Avoid SOQL Queries or DML statements inside FOR Loops

Here Dml statement can be reduced into 1.

List<Property__c> propertyList = new List<Property__c>();

for(Account acc :Trigger.new) {
    Property__c temp =new Property__c();
    //field mapping propery.field = account.field
    propertyList.add(temp);
}

//dml statement outside loop
insert propertyList;
  • I've edited your answer to use proper code & post formatting, as well as fixed a few spelling errors. See Advanced Editing Help for more information on Markdown based formatting. – battery.cord Sep 28 '16 at 16:04
1

When creating a test you need to ask yourself: 'What is the tested code suppose to do'. In this case it looks like your trigger is creating a new Property__c record whenever you create an Account that has Number_of_Properties__c > 0.

So you should be testing four scenarios:

  1. Inserting an Account with Number_of_Properties__c = 0
  2. Inserting an Account with Number_of_Properties__c = 1 (or more)
  3. Inserting an Account with Number_of_Properties__c = 1 (or more) with no Property_Name__c
  4. Inserting an Account with Number_of_Properties__c = 1 (or more) with Property_Name__c

And in each scenario you should be asserting the functionality of the code.

When Number_of_Properties__c = 0 there should be no Property__c record.

When Number_of_Properties__c > 0 there should be a Property__c record that has it's fields set as per the code.

When Property_Name__c is set the Property Name should be the same.

When Property_Name__c is not set the Property Name should be the Account Name.

Others have pointed out other good observations - you are missing the Account.Name in the test. And your trigger itself is poorly optimized.

  • Thanks for all of your help, everyone, especially Stephen. – Mays Sep 28 '16 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.