2

I am trying todo the Super Badge for the Data Integration Specialist. I wrote the Apexcall that will connect to the external system and send out the opportunity information. When trying to write the test class I am getting error

Below is the Apex Class

public class ProjectCalloutService {
    @InvocableMethod
    public static void PostOpportunityToPMS(List<Id> oppId){
        Opportunity listOpp = [Select Id, Name, Account.Name, closedate,amount from Opportunity where Id =: oppId[0]  ];
        
        String inputString = '{"opportunityId":'+listOpp.Id+',"opportunityName":'+listOpp.Name+', "accountName" :'+listOpp.Account.Name+', "closeDate" :'+listOpp.closedate+', "amount" :'+listOpp.amount+'}';
        
        String objToken = ServiceTokens__c.getValues('ProjectServiceToken').Token__c;
        
        System.enqueueJob(new QueueablePMSCall(inputString, objToken, listOpp.Id));       
    }
    
    public class QueueablePMSCall implements Queueable, Database.AllowsCallouts
    {
        String OppId;
        String Token;
        String Input;
        
        public QueueablePMSCall (String input, String objToken, String oppId)
        {
            this.OppId = oppId;
            this.Token = objToken;
            this.Input = input;
        }
        public void execute(QueueableContext context)
        {
            Opportunity opp = [Select Id,StageName from Opportunity where Id =: OppId];
            Http http = new Http();
            HttpRequest httpReq = new HttpRequest();
            httpReq.setBody(Input);
            httpReq.setMethod('POST');
            httpReq.setHeader('Token',Token );
            httpReq.setEndpoint('callout: ProjectService');
            httpReq.setHeader('Content-Type', 'application/json');
            HttpResponse httpRes = http.send(httpReq);
            
            if(httpRes.getStatusCode() == 200)
            {
                opp.StageName = 'Submitted Project';
            }
            else if(httpRes.getStatusCode() != 200)
            {
                opp.StageName = 'Resubmit Project';
            }            
            Update opp;            
        }}}

Below are the Test Classes

@isTest
global class ProjectCalloutServiceMock implements HttpCalloutMock
{
   //Implement http mock callout here 
   global HttpResponse respond(HttpRequest req)
   {
       HttpResponse  res = new HttpResponse();
       res.setStatusCode(200);
       return res;
   }
}

Test Class

@isTest
private class ProjectCalloutServiceTest {
  //Implement mock callout tests here
  @isTest static void testMethod1()
  {
      Account acc = new Account();
      acc.Name = 'Test Account';
      insert acc;
          
      Opportunity opp = new Opportunity();
      opp.Name = 'Test Opp';
      opp.AccountId = acc.Id;
      opp.StageName = 'Prospecting';
      opp.CloseDate=System.Today();
      opp.Amount = 1000;
      insert opp;
      
      List<id> oppId = new List<id>();
      for(Opportunity op : [select id from Opportunity where Id =: opp.Id])
      {
          oppId.add(op.id);
      }
      
      Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock());
      
      Test.startTest();
      ProjectCalloutService.PostOpportunityToPMS(oppId);
      Test.stopTest();
      
      Opportunity ops = [select id,StageName from Opportunity where Id =: opp.Id];
      
      system.assertEquals('Submitted Project', ops.StageName);    
  }
}

When trying to run the test class it throws error on the test class System.NullPointerException: Attempt to de-reference a null object on the test class Class.ProjectCalloutService.PostOpportunityToPMS: line 9, column 1 Class.ProjectCalloutServiceTest.testMethod1: line 27, column 1 not getting what I am missing here. Can anyone please let me know what is that I am missing

1 Answer 1

3

Looks like your test class is missing custom setting records for ServiceTokens__c .

Create a custom setting record using the same name and add test data on the below lines

ServiceTokens__c serviceToken = new ServiceTokens__c();
serviceToken.Name = 'ProjectServiceToken';
serviceToken.Token__c = 'XYZ';
insert serviceToken;

You must log in to answer this question.

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