• rexeus
  • NEWBIE
  • 20 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Guys-

I am testing an apex class (projectCalloutService). I took a different approach and made some class variables static because to avoid having to pass any parameters to queueable interface ( just to try it out). However, my test methods assertion failed in projectCalloutServiceTest class. I use VScode for my dev work and I tried to debug using Apex replay debugger and found out that my Static Variables of the projectCalloutService class becomes null and I dont understand why. Any help is appreciated and most likely I am making a dumb mistake somewhere.. thanks in advance!
 
public class ProjectCalloutService {
    static Opportunity opportunityObj;
    static Opportunity opp ;
    
    
    @InvocableMethod(label='Post Opportunity To PMS' description='Synchronize Outbound Project Data')  
    public static void PostOpportunityToPMS(List<Id> opportunityIds) {
        Opportunity opp = [SELECT Amount,CloseDate,Id,Name,StageName,Account.Name FROM Opportunity WHERE Id =: opportunityIds.get(0)] ; 
        Opportunity opportunityObj;
        ID newJobId = System.enqueueJob(new QueueablePMSCall());
        System.debug(newJobId);
    }
    
    @Future(callout=true) 
    private static void makeServiceCall( ){
        
        opportunityObj = opp;
        JSONGenerator gen = JSON.createGenerator(true); // DO NOT USE JSON as variable (Case Insensitive)
        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();      
        String jsonReq= gen.getAsString();
        System.debug('jsonReq: ' + jsonReq);
        
        
        
        
        ServiceTokens__c token= ServiceTokens__c.getValues('ProjectServiceToken');
        System.debug('TOKEN is :'+token.Token__c);
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('callout:ProjectService/'+token.Token__c);
        System.debug('ENDPOINT is :'+'callout:ProjectService/'+token.Token__c);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody(jsonReq);
        HttpResponse response = http.send(request);
        System.debug('RESPONSE is :'+response);
        
        
        // Parse the JSON response
        if (response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
            opportunityObj.StageName='Resubmit Project';
            upsert opportunityObj;
            
        } else {
            
            System.debug(response.getBody());
            opportunityObj.StageName='Submitted Project';
            upsert opportunityObj;
            
        }
        
        
        
    }
    
    
    
    // Queue 
    class QueueablePMSCall implements Queueable,Database.AllowsCallouts
    {
        
        public void execute(QueueableContext context) {
            
            makeServiceCall();
        }
        
    }
    
    
}
@isTest
private class ProjectCalloutServiceTest {
    
    @TestSetup
    static void makeData(){
        //create the Custom Settings
        ServiceTokens__c servToken = new ServiceTokens__c();
        servToken.Name = 'ProjectServiceToken';
        servToken.Token__c = 'jkhasbdha';
        insert servToken;
        
        Account testAcct = new Account(Name='TestOpAct');
        insert testAcct;
        
        List<Opportunity> oppList = new list<Opportunity>();
        Opportunity testOpp1 = new Opportunity(Name='TestOp1',StageName='Closed Won',Type='New Project',CloseDate=system.today(), AccountId = testAcct.id, Amount=50000);     
        Opportunity testOpp2 = new Opportunity(Name='TestOp2',StageName='Closed Won',Type='New Project',CloseDate=system.today(), AccountId = testAcct.id, Amount=50000);
        System.debug(testOpp1);
        oppList.add(testOpp1);
        oppList.add(testOpp2);
        upsert opplist; 
        System.debug(opplist);
        
        
        
        
        
        System.debug(testOpp1);
        
    }
    
    
    
    @isTest 
    public static void successTest(){    
        
        List<Id> oppIds = new List<Id>(new Map<Id,Opportunity>([SELECT Name,Id FROM Opportunity WHERE Name = 'Testop1']).keySet());
        
        // If an HTTP callout is invoked in test context, the callout is not made. Instead, you receive the mock response that you specify in the respond method implementation in AnimalLocatorMock.
        // Set mock callout class 
        
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock()); 
        ProjectCalloutService.PostOpportunityToPMS(oppIds);
        Test.stopTest();
        
        
        Opportunity opp = [SELECT Name, StageName FROM Opportunity where Name='TestOp1'];
        System.assert(opp.StageName=='Submitted Project');
        
        
    }
    
    @isTest 
    public static void failureTest(){
        
        List<Id> oppIds = new List<Id>(new Map<Id,Opportunity>([SELECT Name,Id FROM Opportunity WHERE Name = 'Testop2']).keySet());
        
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure()); 
        ProjectCalloutService.PostOpportunityToPMS(oppIds);
        Test.stopTest();
        Opportunity opp = [SELECT Name, StageName FROM Opportunity where Name='TestOp2'];
        System.assert(opp.StageName=='Resubmit Project');
        
        
        
    }
    
    
    
}

 
Guys-

I am testing an apex class (projectCalloutService). I took a different approach and made some class variables static because to avoid having to pass any parameters to queueable interface ( just to try it out). However, my test methods assertion failed in projectCalloutServiceTest class. I use VScode for my dev work and I tried to debug using Apex replay debugger and found out that my Static Variables of the projectCalloutService class becomes null and I dont understand why. Any help is appreciated and most likely I am making a dumb mistake somewhere.. thanks in advance!
 
public class ProjectCalloutService {
    static Opportunity opportunityObj;
    static Opportunity opp ;
    
    
    @InvocableMethod(label='Post Opportunity To PMS' description='Synchronize Outbound Project Data')  
    public static void PostOpportunityToPMS(List<Id> opportunityIds) {
        Opportunity opp = [SELECT Amount,CloseDate,Id,Name,StageName,Account.Name FROM Opportunity WHERE Id =: opportunityIds.get(0)] ; 
        Opportunity opportunityObj;
        ID newJobId = System.enqueueJob(new QueueablePMSCall());
        System.debug(newJobId);
    }
    
    @Future(callout=true) 
    private static void makeServiceCall( ){
        
        opportunityObj = opp;
        JSONGenerator gen = JSON.createGenerator(true); // DO NOT USE JSON as variable (Case Insensitive)
        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();      
        String jsonReq= gen.getAsString();
        System.debug('jsonReq: ' + jsonReq);
        
        
        
        
        ServiceTokens__c token= ServiceTokens__c.getValues('ProjectServiceToken');
        System.debug('TOKEN is :'+token.Token__c);
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('callout:ProjectService/'+token.Token__c);
        System.debug('ENDPOINT is :'+'callout:ProjectService/'+token.Token__c);
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody(jsonReq);
        HttpResponse response = http.send(request);
        System.debug('RESPONSE is :'+response);
        
        
        // Parse the JSON response
        if (response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
            opportunityObj.StageName='Resubmit Project';
            upsert opportunityObj;
            
        } else {
            
            System.debug(response.getBody());
            opportunityObj.StageName='Submitted Project';
            upsert opportunityObj;
            
        }
        
        
        
    }
    
    
    
    // Queue 
    class QueueablePMSCall implements Queueable,Database.AllowsCallouts
    {
        
        public void execute(QueueableContext context) {
            
            makeServiceCall();
        }
        
    }
    
    
}
@isTest
private class ProjectCalloutServiceTest {
    
    @TestSetup
    static void makeData(){
        //create the Custom Settings
        ServiceTokens__c servToken = new ServiceTokens__c();
        servToken.Name = 'ProjectServiceToken';
        servToken.Token__c = 'jkhasbdha';
        insert servToken;
        
        Account testAcct = new Account(Name='TestOpAct');
        insert testAcct;
        
        List<Opportunity> oppList = new list<Opportunity>();
        Opportunity testOpp1 = new Opportunity(Name='TestOp1',StageName='Closed Won',Type='New Project',CloseDate=system.today(), AccountId = testAcct.id, Amount=50000);     
        Opportunity testOpp2 = new Opportunity(Name='TestOp2',StageName='Closed Won',Type='New Project',CloseDate=system.today(), AccountId = testAcct.id, Amount=50000);
        System.debug(testOpp1);
        oppList.add(testOpp1);
        oppList.add(testOpp2);
        upsert opplist; 
        System.debug(opplist);
        
        
        
        
        
        System.debug(testOpp1);
        
    }
    
    
    
    @isTest 
    public static void successTest(){    
        
        List<Id> oppIds = new List<Id>(new Map<Id,Opportunity>([SELECT Name,Id FROM Opportunity WHERE Name = 'Testop1']).keySet());
        
        // If an HTTP callout is invoked in test context, the callout is not made. Instead, you receive the mock response that you specify in the respond method implementation in AnimalLocatorMock.
        // Set mock callout class 
        
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock()); 
        ProjectCalloutService.PostOpportunityToPMS(oppIds);
        Test.stopTest();
        
        
        Opportunity opp = [SELECT Name, StageName FROM Opportunity where Name='TestOp1'];
        System.assert(opp.StageName=='Submitted Project');
        
        
    }
    
    @isTest 
    public static void failureTest(){
        
        List<Id> oppIds = new List<Id>(new Map<Id,Opportunity>([SELECT Name,Id FROM Opportunity WHERE Name = 'Testop2']).keySet());
        
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure()); 
        ProjectCalloutService.PostOpportunityToPMS(oppIds);
        Test.stopTest();
        Opportunity opp = [SELECT Name, StageName FROM Opportunity where Name='TestOp2'];
        System.assert(opp.StageName=='Resubmit Project');
        
        
        
    }
    
    
    
}