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
AlaaAlaa 

how to write unit tests for apex classes and triggers

Hi , where can i find the documentation on how to write unit test for my apex classes and triggers?

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Use Test.isRunningTest to skip the callout as callout invoked from the test class will always gives an excetion. Put a condition before your logic for call out. 

 

 

if (!Test.isRunningTest())
{
   // do callout
}
else
{
   // Prepare your fake callout result
}

 

 

All Answers

MJ09MJ09

Check out the Apex Language Reference - http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf

 

Also, in the Search box for this site, search for "unit test." You'll see lots of articles on unit testing.

Shashikant SharmaShashikant Sharma

I can provide you structure

 

 

@isTest
private class testClass()
{
  private static isTestMethod void testApexClass()
  {
  //data creation
  //If controller class has a standard controller then create one instance of it
  //create Controller class instance
test.startTest()

instanceOfClass.invokeMethodToTest();
//assert your results
test.stopTest()
  
   }

 private static isTestMethod void testApexTrigger()
  {
  //data creation
  test.startTest()

// dml action to fire the trigger

//assert your results
test.stopTest()
  
   }
}

 

 

AlaaAlaa

Am doing this correctly and my test runs , but my trigger is calling an apex class methos that calls an http web service so when I run the test it gives me in the log:

FATAL_ERROR|System.TypeException: Methods defined as TestMethod do not support Web service callouts, test skipped

 

so it skips the test because it has a call to web service so what shoudl I do in this case?

Shashikant SharmaShashikant Sharma

Use Test.isRunningTest to skip the callout as callout invoked from the test class will always gives an excetion. Put a condition before your logic for call out. 

 

 

if (!Test.isRunningTest())
{
   // do callout
}
else
{
   // Prepare your fake callout result
}

 

 

This was selected as the best answer
AlaaAlaa

Ok Thank you, now all is Ok and successfuly deployed , now when I run the calss in the production am getting error that am exceeding the 150 limit for DML stataments ... what am doing is trying to update opportunites in the system... first am getting them all in one statament then doing some modification on each object(opportunity) and update it seperatley... thats why am exceeding the 150 limit.. any idea or work arround?

Shashikant SharmaShashikant Sharma

This seems to be a problem with your code, you must be using some dml statement  in a loop. Please provide the code where you are getting this.

AlaaAlaa

Yes it was  aproblem with the code and now fixed.

thank you.

Shashikant SharmaShashikant Sharma

Your welcome Alla