I have a Controller (FooController
) which calls an @future
method (bar()
) belonging to another class (FooServices
).
Ideally in my test class for this Controller (FooControllerTest
) I only want to test that the @future
method has been called, I don't want to test its effects as that's FooServicesTest
's job.
I know that I can use Test.startTest()
and Test.stopTest()
to see the effects of an @future
method in a test class, but is there any way of identifying that a @future
method has been called/queued up in a test class?
Since code is generally easier to understand than words, here is an example of what I would like to be able to do:
FooController
public class FooController
{
public PageReference save()
{
// Do some operations...
// Call my @future method
FooServices.bar();
return null;
}
}
FooServices
public class FooServices
{
@future
public static void bar()
{
// Do some things in the future...
}
}
FooControllerTest
@isTest
private class FooControllerTest
{
@isTest
private static void save_ValidData_CalloutQueued()
{
// Setup my test data...
FooController controller = new FooController();
controller.save();
// Is this part possible? THE BELOW METHOD NAME IS ENTIRELY FICTIONAL
System.assert(System.isFutureJobQueued('FooServices.bar'));
}
// OR...
@isTest
private static void save_ValidData_CalloutExecuted()
{
FooController controller = new FooController();
Test.startTest();
controller.save();
Test.stopTest();
// Yet another fictional method...
System.assert(System.hasFutureJobExecuted('FooServices.bar'));
}
}