function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jordan Lee 13Jordan Lee 13 

Object created in unit test not showing created fields in class being tested

I have created two classes and a test class to test these classes. The test class creates an Opportunity object and populates a few fields. It continues to pass the id of the opportunity to the class being tested. When a SOQL query is run with the pass Id, none of the other fields included in the SOQL query are being populated.

Here is the test class:
@isTest
public class TestGatewayBalancer {
    @isTest public static void AssignGatewayWithLowestRatio() {
        Opportunity opp = new Opportunity(Name = 'Test', StageName = 'New', CloseDate = Date.Today());
        opp.ChargentSFA__Charge_amount__c = 100;
        Database.insert(opp);
    System.debug(loggingLevel.ERROR, '=> Opp being passed from test to upfront class: ' + opp);

        Test.StartTest();
        UpfrontGatewayBalancer.manualCharge(opp.Id);
        Test.StopTest();

        System.assertEquals(gateway.Id, opp.ChargentSFA__Gateway__c, 'The gateway is incorrect.');
    }

The class that is being tested:
global with sharing class UpfrontGatewayBalancer {
    webService static Boolean manualCharge(Id oppID) {
        Opportunity opp = [SELECT Id, ChargentSFA__Gateway__c, ChargentSFA__Charge_Amount__c FROM Opportunity WHERE Id = :oppID];
        //Find and assign appropriate gateway
        System.debug(loggingLevel.ERROR, '=> Opp being passed from upfront to the handler class: ' + opp);
        GatewayLoadBalancerUtility.OpportunityHandler(opp);
        //Check if gateway was assigned before returning
        if(opp.ChargentSFA__Gateway__c == null) {
            return false;
        }
        else {
            return true;
        }
    }

And the messages I get in the execution log are the following:
13:10:25:909 USER_DEBUG [26]|ERROR|=> Opp being passed from test to upfront class: Opportunity:{Name=Test, StageName=New, CloseDate=2016-11-14 00:00:00, ChargentSFA__Charge_Amount__c=100, Id=00655000007bmnWAAQ}

13:10:26:005 USER_DEBUG [14]|ERROR|=> Opp being passed from upfront to the handler class: Opportunity:{Id=00655000007bmnWAAQ}

So clearly the object has those fields getting set in the test class, but they aren't pulled down from the SOQL query in the class being tested. If anybody has any ideas as to what's going on here I would appreciate the help.
 
Best Answer chosen by Jordan Lee 13
Jordan Lee 13Jordan Lee 13
I found what the issue was. The charge amount field is dependent on another field being set. Since it is a formula field it was being overwritten after my insert. After setting the other field, everything worked as expected with my original code.

All Answers

surya kanadhipatlasurya kanadhipatla
Test class is fine but actual web service needs to be written per below link especially oppID as 

01global with sharing class UpfrontGatewayBalancer {
global class OpportnityInfo {
        WebService String oppID ; 
    }
02    webService static Boolean manualCharge(OpportnityInfo oppinfo) {
03        Opportunity opp = [SELECT Id, ChargentSFA__Gateway__c, ChargentSFA__Charge_Amount__cFROM Opportunity WHERE Id = :oppID];
04        //Find and assign appropriate gateway
05        System.debug(loggingLevel.ERROR, '=> Opp being passed from upfront to the handler class: ' + opp);
06        GatewayLoadBalancerUtility.OpportunityHandler(opp);
07        //Check if gateway was assigned before returning
08        if(opp.ChargentSFA__Gateway__c == null) {
09            return false;
10        }
11        else {
12            return true;
13        }
14    }


Sample:
https://developer.salesforce.com/forums/?id=906F00000008z4VIAQ

 
Jordan Lee 13Jordan Lee 13
Surya, I did as you suggested and I'm getting the exact same result. So now my class being tested looks just like what you wrote, with the exception of line 3 being changed to insert the parameter :oppinfo.oppID instead of :oppID. Also in my test class I changed lines 9 through 11 to this:
Test.StartTest();
UpfrontGatewayBalancer.OppInfo oi = new UpfrontGatewayBalancer.OppInfo();
oi.oppID = opp.Id;
UpfrontGatewayBalancer.manualCharge(oi);
Test.StopTest();
My debug output is still exactly the same. The class is only getting the ID of the Opportunity, not the other fields being set in the test class.
surya kanadhipatlasurya kanadhipatla
Jordan, Can you confirm the following : 1. is test class passed? 2. Did you get 100% coverage or at least SOQL is covered(in the main class) by the test class without issues? I will try to replicate the same in my org and let you know in the evening.
Jordan Lee 13Jordan Lee 13
The test class doesn't pass because the call to GatewayLoadBalancerUtility.OpportunityHandler(opp); needs the values that are not coming through in the query. In the Utility class, my OpportunityHandler method uses the opp.ChargentSFA__Charge_amount__c field being set to 100 in the test class. The problem is that field is null so my test fails with the message: "System.NullPointerException: Argument cannot be null."
surya kanadhipatlasurya kanadhipatla
Jordan, can you double check below line UpfrontGatewayBalancer.OppInfo oi = new UpfrontGatewayBalancer.OppInfo(); I'm thinking it must be UpfrontGatewayBalancer.OpportnityInfo oi = new UpfrontGatewayBalancer.OpportnityInfo ();
Jordan Lee 13Jordan Lee 13
Not sure what exactly you mean by double checking oi, but I put this a debug output right after that line and the oi object returned the following:
13:42:03:997 USER_DEBUG [31]|ERROR|=> OppInfo object being passed to upfront class: OppInfo:[oppID=00655000007bpEgAAI]
surya kanadhipatlasurya kanadhipatla
Jordan,
There are few issues with your code, please compare and correct them.

I tried the below code same as yours in my Org and able to see the field values from actual class:

Webservice class:
==================================================================
global class UpfrontGatewayBalancer {    
    global class OpportnityInfo {        
        WebService String oppID ; 
    }    
    webService static Boolean manualCharge(OpportnityInfo oppinfo) {   
            Id id1 = oppinfo.oppID;
        Opportunity opp = [SELECT Id,ChargentSFA_Gateway__c,ChargentSFA_Charge_Amount__c FROM Opportunity WHERE id=:id1 ];
        
        //Find and assign appropriate gateway
        System.debug('ChargentSFA_Gateway__c field value in the actual class: ' + opp.ChargentSFA_Gateway__c);
        System.debug('ChargentSFA_Charge_Amount__c field value in the actual class: ' + opp.ChargentSFA_Charge_Amount__c);
        //Check if gateway was assigned before returning
        
        if(opp.ChargentSFA_Gateway__c == null) {
            return false;            
        }
        
        else {
            
            return true;
        }
    }
    
}

==================================================================

Test class which i used:
==========================================
@isTest
public class TestGatewayBalancer {
    @isTest public static void AssignGatewayWithLowestRatio() {
        Opportunity opp = new Opportunity(Name = 'Test2000', StageName = 'New', CloseDate = Date.Today());
        opp.ChargentSFA_Charge_amount__c = 100;
        opp.ChargentSFA_Gateway__c = 'testing Gate way';
        Database.insert(opp);
        System.debug('=> Opp being passed from test to upfront class: ' + opp);
        Test.StartTest();
        UpfrontGatewayBalancer.OpportnityInfo oi = new UpfrontGatewayBalancer.OpportnityInfo();
        oi.oppID = opp.Id;
        UpfrontGatewayBalancer.manualCharge(oi);      
        Test.StopTest();
        
    }
}
========================================================================
Debug Logs for your reference 

===========================================

15:15:48.1 (193490266)|VARIABLE_SCOPE_BEGIN|[7]|opp|Opportunity|true|false 15:15:48.1 (193526127)|VARIABLE_ASSIGNMENT|[7]|opp|{"s":1,"v":{"Id":"006i000000h4HLBAA2","ChargentSFA_Gateway_ (2 more) ...":"testing Gate way","ChargentSFA_Charge_A (8 more) ...":100}}|0x7f72f6e5 15:15:48.1 (193534204)|STATEMENT_EXECUTE|[10] 15:15:48.1 (193539271)|HEAP_ALLOCATE|[10]|Bytes:56 15:15:48.1 (193587475)|HEAP_ALLOCATE|[10]|Bytes:72 15:15:48.1 (193631191)|USER_DEBUG|[10]|DEBUG|ChargentSFA_Gateway__c field value in the actual class: testing Gate way 15:15:48.1 (193642582)|STATEMENT_EXECUTE|[11] 15:15:48.1 (193647118)|HEAP_ALLOCATE|[11]|Bytes:62 15:15:48.1 (193725263)|HEAP_ALLOCATE|[11]|Bytes:3 15:15:48.1 (193741526)|HEAP_ALLOCATE|[11]|Bytes:65 15:15:48.1 (193753809)|USER_DEBUG|[11]|DEBUG|ChargentSFA_Charge_Amount__c field value in the actual class: 100 15:15:48.1 (193765967)|STATEMENT_EXECUTE|[18] 15:15:48.1 (193769403)|STATEMENT_EXECUTE|[20] 15:15:48.1 (193779808)|METHOD_EXIT|[12]|01pi00000073Rzu|UpfrontGatewayBalancer.manualCharge(UpfrontGatewayBalancer.OpportnityInfo)
Jordan Lee 13Jordan Lee 13
I found what the issue was. The charge amount field is dependent on another field being set. Since it is a formula field it was being overwritten after my insert. After setting the other field, everything worked as expected with my original code.
This was selected as the best answer
surya kanadhipatlasurya kanadhipatla
Glad to know that Jordan!