0

I'm trying to write a test class for the following trigger:

trigger AccountNumberUpdate on Account(before insert, before update) {
    AccNumber__c value = new accNumber__c(Name='1',Ones__c=100000);

    for(accNumber__c record : 
        [Select 
            Id
            ,Name
            ,Ones__c 
         From 
             AccNumber__c WHERE Name='1'FOR UPDATE]) {
        value = record;
    }

    for(Account record:Trigger.new) {
        if(record.Region__c=='Americas' && record.AccountNumber==null) {
            record.AccountNumber ='0'.repeat(math.max(0,0-String.valueOf(value.Ones__c).length()))+String.valueOf(value.Ones__c);
            value.Ones__c+=1;
        }
    }

    update value;
}

Here's what I have so far for a test class but i'm only getting 62%. The trigger assigns an Account Number when an account is created or updated if the region=americas and the AccountNumber field is null.

My class:

@isTest
private class UnitTests_AccountNumberUpdate{

    static testMethod void myUnitTest(){
        // Setup the Account record
        Account acc = new Account(
            Name = 'test account'
            ,Region__c = '--None--'
            ,AccountNumber = null
        );
        insert acc;

        Test.startTest();
        acc.Region__c = 'Americas';
        update acc;
        Test.stopTest();
    }
 }
  • 1
    You need to examine SOQL "where" clauses and "if" conditions in your trigger and ensure that you supply field values in the test objects you insert or update that cover the permutations in one or more test methods. – Keith C May 29 '14 at 16:22
1

It looks like you never insert an accNumber__c record in your test so the following for loop never gets run in your test:

for(accNumber__c record:[SELECT Id,Name,Ones__c FROM accNumber__c WHERE Name='1'FOR UPDATE])
{
    value = record;
}
0

Thank you guys! Finally got 100% coverage!

 @isTest
  private class UnitTests_AccountNumberUpdate {

  static testMethod void myUnitTest() {
     //Setup accNumber record
     accNumber__c accno = new accNumber__c();
     accno.Name = '1';
     accno.Ones__c=100000;
     insert accno;

     // Setup the Account record
     Account acc = new Account();
     acc.Name = 'test account';
     acc.Region__c = '--None--';
     acc.AccountNumber =null;
     insert acc;

      test.startTest();
          acc.Region__c = 'Americas';
          update acc;
      test.stopTest();
    }
  }

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.