You need to sign in to do that
Don't have an account?
Jordan 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:
The class that is being tested:
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.
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.
All Answers
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
13:42:03:997 USER_DEBUG [31]|ERROR|=> OppInfo object being passed to upfront class: OppInfo:[oppID=00655000007bpEgAAI]
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)