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
chrisjanning2chrisjanning2 

testmethod on @future

I have one method in a class that is designated as @future.  How do I add test coverage for it?  I'm getting "Save error: Test method cannot be marked as future"

Starz26Starz26

Test methods are no @future.

 

if you are trying to execute a test that call an @future class then place it within the test.startTest() and test.stopTest() code.

 

The future will run at the stopTest() call and then you can test for the results.

 

 

sfdcfoxsfdcfox

To clarify, given a function:

 

@future
public static void doSomething() {

}

It would be tested as:

 

@isTest
public static void testDoSomething() {
  Test.startTest();
  MyClass.doSomething();
  Test.stopTest(); // doSomething will run here
  // Check results of MyClass.doSomething here
}

 

chrisjanning2chrisjanning2

Does testDoSomething need to be in the same or a separate classe?

chrisjanning2chrisjanning2

I'm also trying to create an AppExchange package and I'm getting this ... 

 

Upload Failed
Average test coverage across all Apex Classes and Triggers is 6%, at least 75% test coverage is required.
sfdcfoxsfdcfox

Does testDoSomething need to be in the same or a separate classe?


It doesn't matter. Put it where you will. Some developers like the "all-in-one" approach to testing (fewer classes), some like "separation of testing" (more classes that are smaller).

 

I'm also trying to create an AppExchange package and I'm getting this ... 

 

Upload Failed
Average test coverage across all Apex Classes and Triggers is 6%, at least 75% test coverage is required.
 
You need to get your total coverage over 75%, as it says. Make sure you're only uploading classes that are part of the package (instead of all classes), and make sure those classes and triggers are well-covered.
chrisjanning2chrisjanning2

My code coverage is still very low.  This is what I've added to my GetPostData class... 

 

public with sharing class GetPostData {

 

@isTest
public static void testDoSomething() {
Test.startTest();
execute();
Test.stopTest();
}

@future(callout=true)
public static void execute() {

...

all logic is in here including some http callouts

...

}

} //end class

 

I have 7 or 8 classes like this structured in the exact same format.  

 

Thanks for all of the help.

sfdcfoxsfdcfox
Functions with callouts fail in all but the latest version (you need to use a mock call setup). The usual means of testing is to write the function such that it won't actually call out, which will terminate the script; it's a mechanism to make sure your tests are not dependent on a third party server that might not even be responding at that time, and also to make sure that that server won't attempt to process the request and treat it as a live request.