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
SalesForce DummySalesForce 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.

Best Answer chosen by Admin (Salesforce Developers) 
Mike@COLMike@COL

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:

 

//Create method variables
Account act = newAccount();
Contact con = newContact();
//Call test class that contains object methods
TestCreate tca = newTestCreate();
tca.getAccount();
tca.getContact();
act = tca.act;
con = tca.con;

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:

public Account getAccount()
    {
     //Create account
        Account act = new Account();
        act.Name = 'test account';
        act.BillingStreet = '123 Main Street';
        act.BillingCity = 'Orlando';
        act.BillingState = 'FL';
        act.BillingPostalCode = '32801';
        insert act;
        return act;
    }

 

 

All Answers

Mike@COLMike@COL

Yes, you can do this. Just make another class and put your methods in there. Do not tag them as TestMethods.

SalesForce DummySalesForce Dummy

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.

Mike@COLMike@COL

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:

 

//Create method variables
Account act = newAccount();
Contact con = newContact();
//Call test class that contains object methods
TestCreate tca = newTestCreate();
tca.getAccount();
tca.getContact();
act = tca.act;
con = tca.con;

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:

public Account getAccount()
    {
     //Create account
        Account act = new Account();
        act.Name = 'test account';
        act.BillingStreet = '123 Main Street';
        act.BillingCity = 'Orlando';
        act.BillingState = 'FL';
        act.BillingPostalCode = '32801';
        insert act;
        return act;
    }

 

 

This was selected as the best answer
SalesForce DummySalesForce Dummy

Awesome!! Thanks a bunch.

eugene_bmeugene_bm

Cool... :smileyvery-happy:

AndyOgnenoffAndyOgnenoff

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.