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
Dan Lambeth 31Dan Lambeth 31 

why does this attempt to create an account from a test class fail?

@testSetup
    static void setup() {
        List<Account> accounts; 
        
        // get some account Ids -- we will just create a couple of dummies
        
        System.debug ('AccountProcessorTest.setup() entered');
        try {
             accounts.add (new Account (name = 'who cares'));
             accounts.add (new Account (name = 'who cares 2'));
             insert accounts;
             System.debug (accounts.size() + ' accounts created');
        }
        catch (Exception e) {
           System.debug('The following exception of type ' + e.getTypeName() + ' has occurred: ' + e.getMessage());
        }

The method consistently fails at the first accounts.add() call.  

09:37:52:002 USER_DEBUG [19]|DEBUG|The following exception of type System.NullPointerException has occurred: Attempt to de-reference a null object

There are no validation rules on account.   I can create this account via the UI supplying nothing but the name.

thanks in advance
Best Answer chosen by Dan Lambeth 31
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please try the below code.

@isTest
public class TestSetupMethodExample {
     @testSetup static void setup() {
         
        List<Account> testAccts = new List<Account>();
		Account ac=new Account();
		ac.name='who cares';
		
		Account acc=new Account();
		acc.name='who cares2';
		
		testAccts.add(ac);
		testAccts.add(acc);
		if(testAccts.size()>0){
		  insert testAccts;
		}
        
      
    }
 
    @isTest static void testMethod1() {
	  List<Account> Accts = new List<Account>();
           Accts  = [SELECT Id FROM Account];
		  system.debug(Accts);
         
    }
	}

Please mark it as the Best Answer if it helps you.

Thank You

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please try the below code.

@isTest
public class TestSetupMethodExample {
     @testSetup static void setup() {
         
        List<Account> testAccts = new List<Account>();
		Account ac=new Account();
		ac.name='who cares';
		
		Account acc=new Account();
		acc.name='who cares2';
		
		testAccts.add(ac);
		testAccts.add(acc);
		if(testAccts.size()>0){
		  insert testAccts;
		}
        
      
    }
 
    @isTest static void testMethod1() {
	  List<Account> Accts = new List<Account>();
           Accts  = [SELECT Id FROM Account];
		  system.debug(Accts);
         
    }
	}

Please mark it as the Best Answer if it helps you.

Thank You

This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi Dan,

You can take reference from the below test class:-
@testSetup
    static void setup() {
        List<Account> accounts= new List<Account>();        
        System.debug ('AccountProcessorTest.setup() entered');
        try {
             accounts.add (new Account (name = 'who cares'));
             accounts.add (new Account (name = 'who cares 2'));
             insert accounts;
             System.debug (accounts.size() + ' accounts created');
        }
        catch (Exception e) {
           System.debug('The following exception of type ' + e.getTypeName() + ' has occurred: ' + e.getMessage());
        }
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

 
Dan Lambeth 31Dan Lambeth 31
Thanks Suraj -- that worked.