0

I have written one trigger for account where i am appending IND to the newly created account name.I have written a test class for the same.When i wrote test class i am creating a testAccount but after DML operation the account name of test account is not getting appended with IND.But it should append as i think.Let me know where i have gone wrong in implementing test class.Below are my code for the same.

Apex Trigger:

trigger AccountConcat on Account (before insert) {
    for(Account a :Trigger.new){
        a.name = 'IND' + a.name;
        System.debug('Value of acc name :::'+a.name);

    }
}

Test class:

@isTest
public class AccountConcat_Test {
    public static testmethod void concattest(){
        Account acc = new Account(name='testAccount');
        insert acc;
        System.debug('val of account name ::::'+acc);
        System.debug('val of account name ::::'+acc.name);
        String expected = 'INDtestAccount';
        System.assertEquals(expected, acc.name);
    }
}
0

First, if you want to append 'IND' to the account.name, you need to change the following line in you trigger as follows:

    a.name = 'IND' + a.name;

becomes

    a.name = a.name + 'IND';

What you're currently doing is pre-pending 'IND' to the account.name.

As for your test class, it should look a bit more like the following:

@isTest
public class AccountConcat_Test {

    public static testmethod void concattest(){

        List<Profile>ps = [select id from Profile where name = 'Standard User' limit 1];

        // Create the user to be used in this test.&nbsp; 

        User U0 = new User(alias ='u0tst', 
        email='standarduser@testorg.com', 
        emailencodingkey='UTF-8', 
        lastname='Testing', languagelocalekey='en_US', 
        localesidkey='en_US', 
        profileid = ps[0].Id, 
        timezonesidkey='America/Los_Angeles', 
        username='standarduser@testorg.com');

        insert U0;

        RunAs(U0){

           integer n=0;

           list<Account> acc = new list<Account>(name='testAccount' + n, OwnerId = U0.Id );
           // add for loop here to get debug when list size greater than 1 (n>0)
           for(integer i=0; i<n; i++){

               System.debug('val of account name ::::'+ acc[i] );

           }

           test.startTest();

           insert acc;

           test.stopTest();   

           for(integer i=0;i<n;;i++){

              System.debug('val of account name ::::'+acc.name);

           } 

           // String expected = 'INDtestAccount';

           for(integer i=0;i<n;;i++){

              System.assertEquals(acc.name, acc.name + 'IND'); 
              //written for appending instead of pre-pending
              //change to 'IND' + acc.name if you actually do want to pre-pend
           }
       }// end runAs
    }//end method
}//end class

Note: Your org may have validation rules that make other fields required, such as shipping address and/or billing address, type, etc. You'll need to check your org's validation rules to determine what fields are required to make certain your test class populates them.

I've added the integer n variable and created everything as lists so you can bulk test your trigger once you know it works for a single record. This is a good practice to get in the habit of.

1
  • its pre-appending only.IND+AccountName=INDAccountName
    – Jack vardy
    Nov 8 '15 at 18:53
0

The objects in memory are not automatically updated when triggers act so in tests you have to re-query to check what happened:

System.assertEquals(expected, [select Name from Account where Id = :acc.Id].Name);

Note that triggers can fire more than once so I suggest your trigger should check for the prefix and only add it if it is not already present.

(Your trigger will impact all users and all apps in the org: seems very intrusive.)

5
  • I am getting error saying "System.AssertException: Assertion Failed: Expected: INDtestAccount, Actual: testAccount".Trigger is not appending IND to account name on insertion of account.
    – Jack vardy
    Nov 8 '15 at 18:45
  • You make a good point on the checking first. As for the test class, in his case, he's not using Test.StartTest or test.StopTest. Once he does, he may not need to query.
    – crmprogdev
    Nov 8 '15 at 18:53
  • @crmprogdev Those calls impact governor limits and collecting async requests - no effect on the query semantics. Suggest you remove the Profile/User/RunAs from your answer: not a good thing to include code that has no purpose.
    – Keith C
    Nov 8 '15 at 19:42
  • It would seem to me that he needs to assign an owner to the Account. Thus the use of a RunAs or he could have just created the user and assigned it as the owner. Either way a user was needed for the assignment.
    – crmprogdev
    Nov 8 '15 at 19:50
  • 1
    @crmprogdev The focus of the test is on naming not on ownership logic so letting the Account be owned by the User running the test is fine and normal
    – Keith C
    Nov 8 '15 at 19:54

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.