You need to sign in to do that
Don't have an account?
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
You have the right idea. Just need to declare the variables as static.
I.E.
All Answers
where is the data inserted for accounts and contacts and opportunities.I mean test data,
You have the right idea. Just need to declare the variables as static.
I.E.
That's great thanks.
In my ignorance I thought that declaring them as static was the same as making them read only.
Thanks.
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?
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.