You need to sign in to do that
Don't have an account?
SalesForce Dummy
Share test class logic
Is it possible to share logic between test classes. The example I have is creating a test class for a trigger on a task. I have to first create an account and then a contact and finally a task. Is there some way to place creation of the account and contact in a separate class and call the class whenever I need to create them? I thought that this was made possible in the last release but I can't find it anywhere.
Your test class isn't calling the methods that populat the act and con variables. Your test class should call getAccount and getContact. Like this:
Side notes:
1) In your test class, you do not need to initialize your account and contact variables with empty objects. You can initialize them to null like this: Account act = null;
2) I would remove the member variables from your TestCreate class, and simply return the objects you create from the methods themselves. Like this:
All Answers
Yes, you can do this. Just make another class and put your methods in there. Do not tag them as TestMethods.
I tried that but when I try to in my code the object is null. Here's example code.
TEST CLASS
//Create method variables
Account act = newAccount();
Contact con = newContact();
//Call test class that contains object methods
TestCreate tca = newTestCreate();
act = tca.act;
con = tca.con;
UTILITY CLASS
public with sharing class TestCreate
{
//Create test variables
public Account act = new Account();
public Contact con = new Contact();
public void getAccount()
{
//Create account
act.Name = 'test account';
act.BillingStreet = '123 Main Street';
act.BillingCity = 'Orlando';
act.BillingState = 'FL';
act.BillingPostalCode = '32801';
insert act;
}
public void getContact()
{
//create contact
con.FirstName = 'Tom';
con.LastName = 'Smith';
con.AccountId = act.Id;
con.MailingStreet = '123 Main Street';
con.MailingCity = 'Orlando';
con.MailingState = 'FL';
con.MailingPostalCode = '32801';
insert con;
}
}
Whenever I attempt to use the variable con or act they are both null.
Your test class isn't calling the methods that populat the act and con variables. Your test class should call getAccount and getContact. Like this:
Side notes:
1) In your test class, you do not need to initialize your account and contact variables with empty objects. You can initialize them to null like this: Account act = null;
2) I would remove the member variables from your TestCreate class, and simply return the objects you create from the methods themselves. Like this:
Awesome!! Thanks a bunch.
Cool... :smileyvery-happy:
And now with Winter '12 you can declare your test utility class as a test class so it doesn't count against your code limits but still have public methods to be used by your tests.