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
danlatdanlat 

Create a set up test method?

Hi All,

 

How can I create a helper method in my test class to accomodate common code? It doesnt seem to be possible?

 

I have this:

 

 

private Opportunity setUpOpportunityToTest() { ... }

 but its gives me an error when I try to call it: using

setUpOpportunityToTest();

 or

this.setUpOpportunityToTest();

 

 ?

 

 

Thanks

Dan

 

 

Message Edited by danlat on 01-19-2010 04:09 AM
Message Edited by danlat on 01-19-2010 04:11 AM
danlatdanlat

In reply to my own message should the above work?

SteveBowerSteveBower

Public class myTesting {

 

Public opportunity setupToTest() { .....}

 

public class doMyFirstTest() {

        myTesting x = new myTesting;

        Opportunity testingOpp = x.setupToTest();

      .....

}

 

} // myTesting

 

 

 

 

or

 

 

Public class myTesting {

   Opportunity testingOpp;

 

   public void setupTestingOpp() {

       testingOpp = new Opportunity(...);

   }

 

   public doMyFirstTest() {

       myTesting x = new myTesting();

       x.setupTestingOpp();

 

       // When you want to use the testing Opp, reference it with x.

       controller.doSomeTestOnAnOpportunity(x.testingOpp);

   }

}

 

 

 

Second method might be more useful if you have a lot of data structures to setup, and if your setup itself may require several methods.

 

Best, Steve.

 

 

 

 

danlatdanlat

Hi,

 

Thanks for the reply.

 

I thought tests had to be marked as Private? and methods with testMethod?

 

Thanks

danlatdanlat

Hi Again,

 

I solved my issue by using a static method, Should have done that first !!

 

Thanks Again

Message Edited by danlat on 01-20-2010 01:45 AM
SteveBowerSteveBower

My apologies... I was just typing it in and I was more focused on explaining the class/data/method structure to create reusable testing structures within a single testing class.  

 

Yes, the outer class should be private.  Yes, you have to use static testMethods.

 

Best, Steve.