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
benwrigleybenwrigley 

variable scope in test class

Hi All,

 

Writing my first test class here and fallen at the first hurdle!

 

To be able to test my class I need to instantiate a load of test objects first of all, so I have written a little class to do that which works fine.

 

The class I want to test is a (will be a) shopping cart that does nothing but instantiate so far.

 

I've started writing the test class to go alongside for my TDD. First I've instantiated all the test objects and written my first test method. However, Force.com IDE complains that objects are not available in my class. So when I try to save  

 

 

@isTest
private class TMDR_ShoppingCartTest {



	testBuildObjects tbo = new testBuildObjects();
	Account a = tbo.createAccount();
	Contact c = tbo.createContact(a.id);
	Opportunity o = tbo.createOpportunity(a.id,c.id);
	OpportunityContactRole ocr = tbo.createOpportunityContactRole(c.id,o.id);
	OpportunityLineItem oli = tbo.createOpportunityLineItem(o.id);
	OpportunityLineItem oli2 = tbo.createOpportunityLineItem(o.id);
	OpportunityLineItem oli3 = tbo.createOpportunityLineItem(o.id);
		
	static testMethod void testInstantiate() {
			
			System.Debug('########Starting Shopping cart tests###########');
			TMDR_ShoppingCart cart = new TMDR_ShoppingCart(o.id);
		
	}

}

 

 

 

 Force.com complains that o.id does not exist!

 

This must be a simple one. Can you help?

 

TIA

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
grigri9grigri9

You have the right idea. Just need to declare the variables as static.

 

I.E.

 

 

@isTest
private class TMDR_ShoppingCartTest {
	
	static Account a;
	static Contact c;
	static Opportunity o;
	static OpportunityContactRole ocr;
	static OpportunityLineItem oli;
	static OpportunityLineItem oli2;
	static OpportunityLineItem oli3;
	
    static void init(){
		testBuildObjects tbo = new testBuildObjects();
		
		a = tbo.createAccount();
		c = tbo.createContact(a.id);
		o = tbo.createOpportunity(a.id,c.id);
		ocr = tbo.createOpportunityContactRole(c.id,o.id);
		oli = tbo.createOpportunityLineItem(o.id);
		oli2 = tbo.createOpportunityLineItem(o.id);
		oli3 = tbo.createOpportunityLineItem(o.id);
    }
    
	static testMethod void testInstantiate() {
			init();
			
			System.Debug('########Starting Shopping cart tests###########');
			TMDR_ShoppingCart cart = new TMDR_ShoppingCart(o.id);
		
	}

} 

 

 

All Answers

krishnagkrishnag

where is the data inserted for accounts and contacts and opportunities.I mean test data,

grigri9grigri9

You have the right idea. Just need to declare the variables as static.

 

I.E.

 

 

@isTest
private class TMDR_ShoppingCartTest {
	
	static Account a;
	static Contact c;
	static Opportunity o;
	static OpportunityContactRole ocr;
	static OpportunityLineItem oli;
	static OpportunityLineItem oli2;
	static OpportunityLineItem oli3;
	
    static void init(){
		testBuildObjects tbo = new testBuildObjects();
		
		a = tbo.createAccount();
		c = tbo.createContact(a.id);
		o = tbo.createOpportunity(a.id,c.id);
		ocr = tbo.createOpportunityContactRole(c.id,o.id);
		oli = tbo.createOpportunityLineItem(o.id);
		oli2 = tbo.createOpportunityLineItem(o.id);
		oli3 = tbo.createOpportunityLineItem(o.id);
    }
    
	static testMethod void testInstantiate() {
			init();
			
			System.Debug('########Starting Shopping cart tests###########');
			TMDR_ShoppingCart cart = new TMDR_ShoppingCart(o.id);
		
	}

} 

 

 

This was selected as the best answer
benwrigleybenwrigley

That's great thanks.

 

In my ignorance I thought that declaring them as static was the same as making them read only.

 

Thanks.

sean*harrisonsean*harrison

Just a couple follow-up questions:

 

Does anyone know why the IDE says the "Variable does not exist" instead of something like "Cannot reference non-static variable from static context" or some such thing? Is this something was can submit to the IDE team?

 

Amy CAmy C
try using "static testMethod" instead of @isTest for the specified method when testing.
Jaap ScheperJaap Scheper
The accepted solution works, because the init() is called within the testMethod.

Please be aware that you can not reuse static variables in different testMethods. You do not want tests to be dependent on each other because they use the same variables.

More background: https://success.salesforce.com/ideaView?id=08730000000Dj51AAC.