variable not declared

Posted on 2012-09-14
Medium Priority
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 28

Accepted Solution

dpearson earned 2000 total points
ID: 38401040
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 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