Skip to main content

I'm trying to complete challenge 5 in Data specialist super badge but i'm getting 'Methods defined as TestMethod do not support Web service callouts' error.

 

ProjectCalloutService.cls:

_____________________

 

public class ProjectCalloutService {

    

    public static final String PROJECT_SERVICE_TOKEN_NAME = 'ProjectServiceToken';

    

    @invocableMethod

public static void postOpportunityToPMS(List<Id> opportunityIds){

        if(opportunityIds.size() > 0){

            System.enqueueJob(new QueueablePMSCall(opportunityIds));

        }

    }

    

    static void makeCallOut(Opportunity opp, String projectServiceToken){

        HttpRequest request = new HttpRequest();

 

        request.setMethod('POST');

        request.setHeader('Content-Type', 'application/json');

        request.setHeader('token', projectServiceToken);

        request.setEndpoint('callout:ProjectService');

        request.setBody(JSON.serialize(new CalloutInformationWrapper(opp)));

       

        HttpResponse response = new Http().send(request);

        

        if(response.getStatusCode() == 201 && response.getStatus() == 'OK'){

            opp.StageName = 'Submitted Project';

        }

        else if(response.getStatusCode() == 500){

            opp.StageName = 'Resubmit Project';

        }

        

        update opp; 

        

    }

    

    static void collectDataAndCallOut(List<Id> opportunitiyIds){

        List<Opportunity> opportunitiesToProcess = [SELECT ID, Name, Account.Name, CloseDate, Amount

                                                   FROM Opportunity

                                                   WHERE Id In :opportunitiyIds];

        if(opportunitiesToProcess.size() > 0){           

            makeCallOut(opportunitiesToProcess[0], ServiceTokens__c.getInstance(PROJECT_SERVICE_TOKEN_NAME).Token__c);

        }

    }

    

    public class QueueablePMSCall implements System.Queueable, Database.AllowsCallouts{

        List<Id> opportunitiyIdsToProcess;

        

        public QueueablePMSCall(List<Id> opportunitiyIds){

            opportunitiyIdsToProcess = opportunitiyIds;

        }

        

        public void execute(System.QueueableContext context){

            collectDataAndCallOut(opportunitiyIdsToProcess);

        }

    }

    

    public class CalloutInformationWrapper{

        string opportunityId;

        string opportunityName;

  string accountName;

  string closeDate;

  Decimal amount;

        

        public CalloutInformationWrapper(Opportunity opp){

            this.opportunityId = opp.Id;

            this.opportunityName = opp.Name;

            this.accountName = opp.Account.Name;

            this.closeDate = String.valueOf(opp.CloseDate);

            this.amount = opp.Amount;

        }

}

}

 

ProjectCalloutServiceTest 

____________________

 

@isTest

private class ProjectCalloutServiceTest {

  //Implement mock callout tests here

   @testSetup static void testSetupdata(){

        //create the opportunity record

        Opportunity opp1 = new Opportunity();

        opp1.Name = 'Test Opp1';

        opp1.Type = 'New Project';

        opp1.Amount = 100;

        opp1.CloseDate = Date.today();

        opp1.StageName = 'Submitted Project';

        insert opp1;

        //create the opportunity record

        Opportunity opp2 = new Opportunity();

        opp2.Name = 'Test Opp2';

        opp2.Type = 'New Project';

        opp2.Amount = 200;

        opp2.CloseDate = Date.today();

        opp2.StageName = 'Resubmit Project';

        insert opp2;

        //create the Custom Settings

        ServiceTokens__c servToken = new ServiceTokens__c();

        servToken.Name = 'ProjectServiceToken';

        servToken.Token__c = 'qwertyuiopnjhgft';

        insert servToken;

    }

  

  @isTest

  static void testSuccessMessage(){

      Opportunity opp = [Select Id, Name FROM Opportunity WHERE Name = 'Test Opp1' Limit 1];

      List<Id> lstOfOppIds = new List<Id>();

      lstOfOppIds.add(opp.Id);

      // Set mock callout class

      Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock());

      // This causes a fake response to be sent

      // from the class that implements HttpCalloutMock. 

      Test.startTest();

          ProjectCalloutService.postOpportunityToPMS(lstOfOppIds);

      Test.stopTest();    

      // Verify that the response received contains fake values        

      opp = [select StageName from Opportunity where id =: opp.Id];

      System.assertEquals('Submitted Project',opp.StageName);     

  }

  

  @isTest

  static void testFailureMessage(){

      Opportunity opp = [Select Id, Name FROM Opportunity WHERE Name = 'Test Opp2' Limit 1];

      List<Id> lstOfOppIds = new List<Id>();

      lstOfOppIds.add(opp.Id);

      // Set mock callout class

      Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure());

      // This causes a fake response to be sent

      // from the class that implements HttpCalloutMock. 

      Test.startTest();

          ProjectCalloutService.postOpportunityToPMS(lstOfOppIds);

      Test.stopTest();    

      // Verify that the response received contains fake values        

      opp = [select StageName from Opportunity where id =: opp.Id];

      System.assertEquals('Resubmit Project',opp.StageName);

  }

}

 

#Trailhead  #Trailhead Challenges

1 resposta
0/9000