Ok, this is going to become a quite long question. The main goal is to document strange platform behavior and maybe learn more why this happens.
It all started with that test which failed because setup code called an @future
method which failed. (BTW: I was really confused to learn that @future
are called at all without a Test.stopTest()
which led to this question.)
Fails
@isTest
private class TestSetupTest1 {
@isTest
private static void willFail() {
// Setup
callFailingFutureMethod();
// Verify
...
}
}
More or less by accident I made it work moving the setup code into a @TestSetup
method:
Succeeds
@isTest
private class TestSetupTest2 {
@TestSetup
private static void setup() {
callFailingFutureMethod();
}
@isTest
private static void wontFail() {
// Verify
...
}
}
It seems like the @future
is not called at all or in a separate execution context here. That is my assumption because by adding Test.stopTest()
here I can make it fail again.
Again fails
@isTest
private class TestSetupTest3 {
@TestSetup
private static void setup() {
Test.startTest();
callFailingFutureMethod();
Test.stopTest();
}
@isTest
private static void willAlsoFail() {
// Verify
...
}
}