I had a working Queueable class doing callouts. My unit tests were passing but I had to change my class slightly. I encountered the callout exception and I started tracing down the culprit.
The problem appears to be in the test set up. For some reason creating an opportunity object breaks the test. (The opportunity is not used by the code. I managed to break the older working tests just by adding this one insert).

 static void test_batch(){
    TriggerStopper.stopForTest = true;
    Account oAccount = new Account(Name ='Name');
    insert oAccount;
    //insert new Opportunity(Name='this breaks the test', AccountId = oAccount.Id);

    Test.setMock(WebServiceMock.class , new myMocks.SuccessMockImpl());
    System.enqueueJob(new myBatch());

Funny enough the test also breaks when I move the creation of objects into @testSetup method. I don't understand what the reason could be.
The batch doesn't do any DML before the callout and the test works if I comment out the insertion of opportunity.
It's not the opportunity trigger doing suspicious inserts as it's stopped for the purpose of test set up. I'm really baffled.

How can I fix the test?

  • Do you have a service call out when you insert an account?
    – EricSSH
    Commented Aug 15, 2017 at 20:18
  • No callout, No code runs on inserting any object. Inserting account is fine
    – user682217
    Commented Aug 16, 2017 at 6:16

1 Answer 1


TestSetup tends to act a bit oddly, but you can usually fix such silliness by using Test.startTest in TestSetup.

@testSetup static void setupTest() {
    // ...
@isTest static void doTest() {
    Account oAccount = [SELECT ... FROM Account];
    Opportunity oOpp = [SELECT ... FROM Opportunity];
    // Make sure you add asserts here

Anything you do after Test.startTest and before Test.stopTest in testSetup (including all governor limits) will be cleared for your test methods, which should resolve your issue.

Also, generally speaking, you must use Test.startTest before Test.setMock.

  • I tried different permutation with setMock hoping it makes a difference but it doesn't. Adding Test.startTest(); in the setup didn't help either :(
    – user682217
    Commented Aug 16, 2017 at 6:17
  • Something doggy is going on with Mocks success.salesforce.com/… plus related issue in the content. I bet you this relates to this one that apparently has been sorted success.salesforce.com/issues_view?id=a1p300000008XHBAA2 I guess in my case it's the workflows on Opportunity
    – user682217
    Commented Aug 17, 2017 at 11:45
  • @user682217 the status for that one says it's resolved, but I'll try and reproduce it and see if there's an alternative solution.
    – sfdcfox
    Commented Aug 17, 2017 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .