You need to sign in to do that
Don't have an account?
Test Classes Not Passing - Null pointer exception - Superbadge Data Integration Specialist (Part 3)
Hi all, I seem to be stuck on authoring the class and test classes on the Superbadge Data Integration Specialist. I have authored the ProjectCalloutService plus the test class and HTTPCallouts, but encounter a Null pointer exception error at line 35:1
Anything I should add to the code. I have built the process builder as needed.
Project Callout Service
HTTP Callout Success
Anything I should add to the code. I have built the process builder as needed.
Project Callout Service
public class ProjectCalloutService { public static Id opportunityId; @InvocableMethod public static void postOpportunityToPMS(List<Id> opportunityIds){ opportunityId=opportunityIds.get(0); Opportunity opp=[Select Id,Name, closeDate,amount,Account.Name FROM Opportunity Where Id =: opportunityId]; ID jobID = System.enqueueJob(new QueueablePMSCall(opp)); } public class QueueablePMSCall implements system.Queueable,Database.AllowsCallouts { private String jsonOpp; private Opportunity opportunityObject; public QueueablePMSCall(Opportunity opp) { opportunityObject=opp; JSONGenerator gen = JSON.createGenerator(true); gen.writeStartObject(); gen.writeStringField('opportunityId', opp.Id); gen.writeStringField('opportunityName', opp.Name); gen.writeStringField('accountName', opp.account.Name); gen.writeDateField('closeDate', opp.closeDate); gen.writeNumberField('amount', opp.amount); gen.writeEndObject(); jsonOpp= gen.getAsString(); System.debug('jsonOpp: ' + jsonOpp); } public void execute(QueueableContext context) { ServiceTokens__c token= ServiceTokens__c.getValues('ProjectServiceToken'); System.debug(token.Token__c); // create an HTTPrequest object HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setEndpoint('callout:ProjectService/'+ token.Token__c); req.setHeader('Content-Type', 'application/json'); req.setBody(jsonOpp); // create a new HTTP object Http http = new Http(); HTTPResponse res = http.send(req); if (res.getStatusCode() != 201) { System.debug('Error from ' + req.getEndpoint() + ' : ' + res.getStatusCode() + ' ' + res.getStatus()); Opportunity opportunity1=[Select Id, StageName FROM Opportunity Where Id =: opportunityObject.Id]; opportunity1.StageName='Resubmit Project'; update opportunity1; } else { Opportunity opportunity2=[Select Id, StageName FROM Opportunity Where Id =: opportunityObject.Id]; opportunity2.StageName='Submitted Project'; update opportunity2; } } } }Test Class
@isTest private class ProjectCalloutServiceTest { //Implement mock callout tests here public static testMethod void testSuccess(){ Account acc = new Account(Name='Test Account'); insert acc; Opportunity opp = new Opportunity(Name='Test Opportunity', AccountId=acc.id, CloseDate=System.Today(), Amount=12480.00, Type='New Project', StageName='Qualification'); insert opp; Test.startTest(); Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock()); Test.stopTest(); opp.StageName='Closed Won'; update opp; } public static testMethod void testFailure(){ Account acc = new Account(Name='Test Account'); insert acc; Opportunity opp = new Opportunity(Name='Test Opportunity', AccountId=acc.id, CloseDate=System.Today(), Amount=12480.00, Type='New Project', StageNAme='Qualification'); insert opp; Test.startTest(); Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure()); Test.stopTest(); opp.StageName='Closed Won'; update opp; } }
HTTP Callout Success
global class ProjectCalloutServiceMock implements HttpcalloutMock{ //Implement http mock callout here global HttpResponse respond(Httprequest request){ HttpResponse response = new Httpresponse(); String resBody = '[Status=Created, StatusCode=201]'; response.setHeader('Content-Type','application/json'); response.setBody(resBody); response.setStatusCode(201); return response; } }HTTP Callout Fail
//ProjectCalloutServiceMockFailure global class ProjectCalloutServiceMockFailure implements HttpcalloutMock{ //Implement http mock callout failure here //Implement http mock callout here global HttpResponse respond(Httprequest request){ HttpResponse response = new Httpresponse(); String resBody = '[Status=Created, StatusCode=501]'; response.setBody(resBody); response.setStatusCode(501); return response; } }
I do not see you have added the Custom Setting data creation in your data creation. The custom settings data are not available in test as it can vary from environment to environment and hence manually create it. I also noticed, you have not added a @future(callout=true). See my code below -
ProjectCalloutService
ProjectCalloutServiceTest
All Answers
I do not see you have added the Custom Setting data creation in your data creation. The custom settings data are not available in test as it can vary from environment to environment and hence manually create it. I also noticed, you have not added a @future(callout=true). See my code below -
ProjectCalloutService
ProjectCalloutServiceTest