variable not declared

Posted on 2012-09-14
Last Modified: 2016-02-17
I'm writing some unit tests in apex (salesforce platform), which is a derivative of the Java language.  I have an init() method where I'm generating the data for the unit tests.

In my actual test method, I first call the init() to setup the data before actually running the tests.

The contact variables I've setup do not exist in my test method since I declared the variables inside my static init() method.

I know that I can declare each contact variable as static outside my init method, but I was curious if there is a better way to do this, specifically, declaring the variables on the fly within my test method so it makes the code more concise and I don't have a bunch of variables declared at the top level of the class.  It seems more flexible if I can declare the variables within the init() method and still be able to reference the variables within each of my individual test methods.

Is there a way to do this?

Here is the code:

private class ContactCopyAddressMVNTest {
	static Account testAccount = new Account();
	static Account createTestAccount() {
		testAccount.Name = 'testHousehold';
		return testAccount;
	static Contact createTestContact(String firstname, String lastname, Id accountId) {
		Contact testContact 						= new Contact();
		testContact.FirstName 						= firstname;
		testContact.LastName 						= lastname;
		testContact.AccountId 						= accountId;
		testContact.Copy_Mailing_Address_MVN__c		= false;
		testContact.MailingStreet 					= '123 Main Street';
		testContact.mail_Address_Line_2_MVN__c 		= 'PO Box 123';
		testContact.MailingCity 					= 'Beverly Hills';
		testContact.MailingState 					= 'CA';
		testContact.MailingPostalCode 				= '90210';
		testContact.MailingCountry 					= 'United States';
		testContact.Copy_Shipping_Address_MVN__c	= false;
		testContact.OtherStreet 					= '1001 Hollywood Boulevard';
		testContact.ship_Address_Line_2_MVN__c 		= 'Suite 200';
		testContact.OtherCity 						= 'Los Angeles';
		testContact.OtherState 						= 'CA';
		testContact.OtherPostalCode 				= '90001';
		testContact.OtherCountry 					= 'United States';
		insert testContact;
		return testContact;
	static void init() {
		insert createTestAccount();
		system.assertNotEquals(null, testAccount.Id);
		Contact Fred = createTestContact('Fred', 'Flintstone', testAccount.Id);
		system.assertNotEquals(null, Fred.Id);
		Contact Wilma = createTestContact('Wilma', 'Flintsone', testAccount.Id);
		system.assertNotEquals(null, Wilma.Id);
		Contact Peebles = createTestContact('Peebles', 'Flintstone', testAccount.Id);
		system.assertNotEquals(null, Peebles.Id);
	static testMethod void testMailingAddressUpdatedByHousehold() {
			//Fred.Copy_Mailing_Address_MVN__c = true;

Open in new window


Thanks for any help.
Question by:-Dman100-
    1 Comment
    LVL 26

    Accepted Solution

    If I understand correctly what you're trying to do I think you could do it by introducing another class, e.g. TestData which would contain the test Contacts, something like:

    private static class TestData {
       private final Map<String, Contact> m_TestContacts = new HashMap<String,Contact>() ;

       public void addContact(Contact c) { m_TestContacts.put(c.FirstName,c) ; }
       public Contact get(String name) { return m_TestContacts.get(name) ; }

    And then init() becomes:

    private static TestData init() {
        // Create Fred etc. as before
        TestData data = new TestData() ;
         data.addContact(Fred) ;
         data.addContact(Wilman) ;
         return data ;

    Your tests become something like this:

    private static void myTest() {
         TestData data = init() ;
         test.startTest(data.get("Fred")) ;

    Now you no longer have static variables and you could build multiple different "init" methods to create different sets of test data.

    Hope that helps,


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Note: You must have administrative privileges in order to create/edit Sharing Rules. ( (SFDC) is a cloud-based customer relationship management (CRM) system. It is a database most commonly used by sales an…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now