• Shilpa Malviya
  • NEWBIE
  • 5 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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);
    }
}