Find your content:

Search form

You are here

Why does my assertion not fail when I call it after stopTest()?

 
Share

I have opportunity code that calls a future method. While testing it, I have run into some odd behaviour. I cannot get my assertion to fail if it occurs after the stopTest() call. In fact, it seems that no code (even assertions) is being executed after the stopTest() call.

After troubleshooting and debugging, I have boiled my test method down to this (note that the opportunity and contact are static class members):

public static testMethod void checkIntegrationUpdates()
{
    Test.startTest();
    opportunity.StageName = 'Closed Won';
    opportunity.OnBoarding_Type__c = 'Test Type'
    opportunity.Primary_Contact__c = contact.Id;
    Database.Saveresult updateResult = Database.update (opportunity);
    System.debug('Update Result: ' + updateResult.isSuccess());
    Test.stopTest();
    System.assertEquals(1,2);
}

If I move the assertion to before the stopTest() call, then the assertion correctly fails. If I move it to the line after the stopTest() call, as above, the assertion does not get called and the test passes.

Why does the assertion after the stopTest() call not get called? Isn't the correct way to test future methods to make all your assertions after the stopTest() call?


Attribution to: Paddyslacker

Possible Suggestion/Solution #1

Current Thoughts..

Ok after trying to spend some time reproducing what I suspect now might be a platform bug. I've got the following ideas for you to try, but nothing concreate.

  • If you comment out the cal to your future method in the trigger does it work?
  • If so, add it back in and remove gradually some parts of your future method
  • Run your code interactively, does the Apex Job UI show any failures in the future method?
  • Try to move code from your static initialisation into the scope of the test method if you can, I've known statics setup in tests cause some interesting side effects. Including if you can other static initialisers from other classes. The order of these is so dependent on the class loader I often try to avoid them.
  • If all else fails you appear to have a very good repo for a Salesforce Support case... :(

Update:

Since it appears that making Web Service callouts from Future methods in test situations causes this kind of silent failure. You might want to checkout this new feature in Winter'13, http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm

Reproduction Attempts

This works for me (not to keen on statics in tests tbh, but relfected what I assume you have in yours below). I suspect its an exception in your future call, are you doing this in a trigger?

static Opportunity opportunity;

static
{
    opportunity = new Opportunity();
    opportunity.Name = 'Test';
    opportunity.StageName = 'Closed Won';
    opportunity.CloseDate = System.today();
    insert   opportunity;           
}

public static testMethod void checkIntegrationUpdates()
{
    Test.startTest();
    opportunity.StageName = 'Closed Won';
    //opportunity.OnBoarding_Type__c = 'Test Type'
    //opportunity.Primary_Contact__c = contact.Id;
    Database.Saveresult updateResult = Database.update (opportunity);
    System.debug('Update Result: ' + updateResult.isSuccess());
    Test.stopTest();
    System.assertEquals(1,2);
}

Gives me...

enter image description here

By adding this...

@Future
public static void doSomethingInTheFuture()
{
    throw new FutureException('Stop in the future!');
}

public class FutureException extends Exception
{

}

And this...

trigger FutureOpp on Opportunity (before update) {
 TestTest.doSomethingInTheFuture();
}

And then this..

enter image description here

Which as you say should work, I wondering it its throwing an uncatchable exception?


Attribution to: Andrew Fawcett
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4567

My Block Status

My Block Content