You need to sign in to do that
Don't have an account?
Dumb OOP question
Hey all,
I'm still getting the hang of object oriented programming, and I'm not sure how to solve this problem without "global variables".
Basically I have a large testing class. There is a seperate sub class for each trigger, page, and class that need to be tested. A lot of these tests though need the same type of data (contacts, accounts, leads, etc). How can I create one contact, one account, one lead, and let all the sub classes read from it and write to it? These being test classes, I cannot control what data is passed to them. So for example I have something like.
public static contact createTestContact() { Contact testContact = new Contact(Firstname='Frank', Lastname='Jones'); insert testContact; return testContact; } public testMethod void testContactTrigger1() { Contact myContact = createTestContact(); //Do some stuff } public testMethod void testContactOtherTrigger() { Contact myContact = createTestContact(); //Do some different stuff }
How can I let testContactTrigger and testContactOtherTrigger share the same contact? I don't need two copies of the same contact, or the additional overhead that comes from instantiating it twice. Just once would be fine. Can I modify the createTestContact method to know when it has already created a contact once, and just return the same contact next time it is called?
I would create a "TestDataHelper" class that has static methods (very similar to the one you have below) and use "private" variables that will return same instance of contact when not "null" in your "createTestcontact" method.
However if your trigger/page/controllers require a new contact record, i would probably create a new contact record for every test method that requires it.
While there's a minor overhead during test execution process,it does'nt impact the performance of the final apex/trigger code when they execute in your production environment.
Thanks for the reply. Would you be willing to provid a small code sample of what you are thinknig about? I kinda think I get what you are saying, but not totally. Thanks for the help.
Here's an example
Then in your test method you can do something like this:
Of course you can also INSERT them in the TestHelper class if you need to.
Hope that helps!
Ah, perfect thank you. With my real code I was close. I just didn't declair the actual variables outside of the class. Got it, thanks!
Okay, I'm exhausted. I've been workong on this testing class for 3 days now (on and off) and I am just not making much progress. I though declaring my variables as public static in the super class would make them accessible to all the child classes, but that is not the case. I feel like this thing has gotten overly complex and horribly inflexible. Right now the errors I am getting are all basically "error attempting to de-reference a null object" when one of my helper classes tries to use a variable that was set in another helper class. I feel very lost and I don't know what I'm doing. Any input would be much appreciated. Thanks.