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
Mayur MehtaMayur Mehta 

Data Integration Specialist superbadge challenge #4

I am stuck with Data Integration Specialist superbadge challenge #4. While trying to run the test i am getting an error  'Methods defined as TestMethod do not support Web service callouts'. Below are my code:

@IsTest
public class ProjectCalloutServiceMock implements HttpCallOutMock{
   //Implement http mock callout here
    public HttpResponse respond(HttpRequest req)
    {
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setStatus('OK');
        res.setStatusCode(201);
        return res;
    }
}

@Istest
public class ProjectCalloutServiceMockFailure implements HttpCallOutMock {
   //Implement http mock callout failure here
   public HttpResponse respond(HttpRequest req)
    {
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setStatus('Error');
        res.setStatusCode(500);
        return res;
    }
}

@isTest
private class ProjectCalloutServiceTest {
  //Implement mock callout tests here
  @testsetup
  public static void setupdata()
  {
      List<Opportunity> oppsToInsert = new List<Opportunity>();
      
      Account acct = new Account();
      acct.Name='test Account';
      insert acct;
      
      Opportunity opp1 = new Opportunity();
      opp1.Name = 'Opp1';
      opp1.Type='New Customer';
      opp1.AccountId = acct.id;
      opp1.amount=500;
      opp1.CloseDate = date.today();
      opp1.StageName = 'Prospecting';
      oppsToInsert.add(opp1);
      
      Opportunity opp2 = new Opportunity();
      opp2.Name = 'Opp2';
      opp2.Type='New Customer';
      opp2.AccountId = acct.id;
      opp2.amount=2500;
      opp2.CloseDate = date.today().addDays(-3);
      opp2.StageName = 'Prospecting';
      oppsToInsert.add(opp2);
      
      insert oppsToInsert;
      
      ServiceTokens__c st = new ServiceTokens__c();
      st.Name = 'ProjectServiceToken';
      st.Token__c = 'thisistesttoken';
      insert st;
      
  }
    
   @istest
    public static void testSuccessMessage()
    {
        Opportunity oppList = [Select Id from Opportunity where Name='Opp1' limit 1];
        List<Id> oppIds = new List<Id>();
        oppIds.add(oppList.Id);
        Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock());
        test.startTest();
            ProjectCalloutService.postOpportunityToPMS(oppIds);
        test.stopTest();
        oppList = [Select StageName from Opportunity where Name='Opp1' limit 1];
        system.assertEquals('Submitted Project',oppList.StageName);
    }
    
    @istest
    public static void testFailureMessage()
    {
        Opportunity oppList = [Select Id from Opportunity where Name='Opp2' limit 1];
        List<Id> oppIds = new List<Id>();
        oppIds.add(oppList.Id);
        Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure());
        test.startTest();
            ProjectCalloutService.postOpportunityToPMS(oppIds);
        test.stopTest();
        oppList = [Select StageName from Opportunity where Name='Opp2' limit 1];
        system.assertEquals('Resubmit Project',oppList.StageName);
    }
}
VinayVinay (Salesforce Developers) 
Hi,

We have a separate Trailhead team who can help you with these issues. So, can you please use the below link to reach out to them so that one of the agent will get in touch with you.

Support:https://trailhead.salesforce.com/help

Thank you!

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks,
Vinay Kumar
Shilpa MalviyaShilpa Malviya
Hey, 

When I removed the setup data method and did the setup in test methods it worked.
SUDIP KARMAKAR 12SUDIP KARMAKAR 12
Solution: Remove testSetup and instead create your Test data inside test methods... It worked for me. Don't know exactly why Callout don't support testSetup, please add your comments if you know the reason... 
  
@isTest
private class CalloutServiceTest {       
    /*@testSetup
    static void setup(){}
    */
    
    @isTest
    static void testMethod(){
          Account acc = new Account(Name='Test Account');
          insert acc;
          Test.startTest();
           Test.setMock(HttpCalloutMock.class, new CalloutServiceMock());
          Test.stopTest();
     }
 }


ThankYou
Raksha Jha 7Raksha Jha 7
HI, I am facing the same issue, even after creating the test data inside the test method it is not working. can you please post your code, so that i will know what mistake i am doing.

Thank you
 
Raksha Jha 7Raksha Jha 7
Hi, I am able to fix the issue. it is not link with @Test Setup. Test setup is working fine.In test, the Queueable is queued and executed, but it was not triggering any additional asynchronous calls in a Test. So, We must to call the future method explicitly to have it covered and executed.