You need to sign in to do that
Don't have an account?
Livia Pimentel
Test Http Callout - Trigger
Hello, everyone!
I am trying to implement a test class for a trigger, that calls a http method (which comes from this tutorial: https://cloud.google.com/architecture/calling-protected-cloud-functions) .
In my case, everytime when the custom object 'Proposta__c' is updated, the GCPManageCustomersService method is called. Basically, this class implements the calling of a cloud functions that was set up by following the aforementioned tutorial.
When implementing the ' GCPMockHTTPResponseGenerator' class on my test class 'GCPManageCustomersServiceTest', I always get assertion errors on the following lines:
Below, I show you the complete test class:
Finally, the trigger that does the http callout:
I appreciate any help you can provide.
Best regards,
I am trying to implement a test class for a trigger, that calls a http method (which comes from this tutorial: https://cloud.google.com/architecture/calling-protected-cloud-functions) .
In my case, everytime when the custom object 'Proposta__c' is updated, the GCPManageCustomersService method is called. Basically, this class implements the calling of a cloud functions that was set up by following the aforementioned tutorial.
When implementing the ' GCPMockHTTPResponseGenerator' class on my test class 'GCPManageCustomersServiceTest', I always get assertion errors on the following lines:
System.assertEquals(1, asyncJobList.size()); System.assertEquals(true, mock.methodCalled);
Below, I show you the complete test class:
@isTest public class GCPManageCustomersServiceTest { static testmethod void testQueueable() { GCPMockHttpResponseGenerator mock = new GCPMockHttpResponseGenerator(); Test.setMock(HttpCalloutMock.class, mock); Test.startTest(); PriceBook2 pb_se_conv = new PriceBook2(); pb_se_conv.Name = 'SE/CO - CONV'; pb_se_conv.Pre_o_2023_R_MWh__c = 123.45; insert pb_se_conv; PriceBook2 pb_se_I0 = new PriceBook2(); pb_se_I0.Name = 'SE/CO - I0'; insert pb_se_I0; PriceBook2 pb_se_I100 = new PriceBook2(); pb_se_I100.Name = 'SE/CO - I100'; insert pb_se_I100; PriceBook2 pb_se_I5 = new PriceBook2(); pb_se_I5.Name = 'SE/CO - I5'; insert pb_se_I5; PriceBook2 pb_se_INE = new PriceBook2(); pb_se_INE.Name = 'SE/CO - INE'; insert pb_se_INE; PriceBook2 pb_NE_conv = new PriceBook2(); pb_NE_conv.Name = 'NE/CO - CONV'; insert pb_NE_conv; PriceBook2 pb_NE_I0 = new PriceBook2(); pb_NE_I0.Name = 'NE/CO - I0'; insert pb_NE_I0; PriceBook2 pb_NE_I100 = new PriceBook2(); pb_NE_I100.Name = 'NE/CO - I100'; insert pb_NE_I100; PriceBook2 pb_NE_I5 = new PriceBook2(); pb_NE_I5.Name = 'NE/CO - I5'; insert pb_NE_I5; PriceBook2 pb_NE_INE = new PriceBook2(); pb_NE_INE.Name = 'NE/CO - INE'; insert pb_NE_INE; PriceBook2 pb_S_conv = new PriceBook2(); pb_S_conv.Name = 'S/CO - CONV'; insert pb_S_conv; PriceBook2 pb_S_I0 = new PriceBook2(); pb_S_I0.Name = 'S/CO - I0'; insert pb_S_I0; PriceBook2 pb_S_I100 = new PriceBook2(); pb_S_I100.Name = 'S/CO - I100'; insert pb_S_I100; PriceBook2 pb_S_I5 = new PriceBook2(); pb_S_I5.Name = 'S/CO - I5'; insert pb_S_I5; PriceBook2 pb_S_INE = new PriceBook2(); pb_S_INE.Name = 'S/CO - INE'; insert pb_S_INE; PriceBook2 pb_N_conv = new PriceBook2(); pb_N_conv.Name = 'N/CO - CONV'; insert pb_N_conv; PriceBook2 pb_N_I0 = new PriceBook2(); pb_N_I0.Name = 'N/CO - I0'; insert pb_N_I0; PriceBook2 pb_N_I100 = new PriceBook2(); pb_N_I100.Name = 'N/CO - I100'; insert pb_N_I100; PriceBook2 pb_N_I5 = new PriceBook2(); pb_N_I5.Name = 'N/CO - I5'; insert pb_N_I5; PriceBook2 pb_N_INE = new PriceBook2(); pb_N_INE.Name = 'N/CO - INE'; insert pb_N_INE; Account acc = new Account(); acc.Name = 'ACC2'; acc.OwnerId = UserInfo.getUserId(); insert acc; Opportunity opp = new Opportunity(); opp.Name = 'TEST OPP 27_05_22'; opp.AccountId = acc.Id; opp.CloseDate = date.today(); opp.StageName = 'Proposta'; insert opp; RFQ__c rfq = new RFQ__c(); rfq.Oportunidade__c = opp.Id; rfq.Respons_vel__c = UserInfo.getUserId(); date inicio = date.today(); rfq.In_cio_Suprimento__c = date.today(); rfq.Validade_Suprimento__c = inicio.addMonths(15); rfq.Tipo_de_Energia__c = 'Conv'; rfq.Ponto_de_Entrega__c = 'SE/CO'; insert rfq; RFQ__c rfq_inserted = [SELECT Id, Ponto_de_Entrega__c, Tipo_de_Energia__c FROM RFQ__c WHERE Id=:rfq.Id]; Proposta__c proposta_first = [SELECT Name, Id,Pre_o_Ano1_Tabela__c, Cat_logo_de_pre_os__c FROM Proposta__c WHERE RFQ__c=:rfq_inserted.Id]; proposta_first.CNPJ__c = '123456'; update proposta_first; Test.stopTest(); List<AsyncApexJob> asyncJobList = [select id, ApexClass.Name, JobType, Status from AsyncApexJob]; PriceBook2 pb_correspondente = [SELECT Id, Name from PriceBook2 WHERE Id=:proposta_first.Cat_logo_de_pre_os__c]; System.assertEquals(pb_correspondente.Name, 'SE/CO - CONV' ); System.assertEquals(proposta_first.Pre_o_Ano1_Tabela__c, 123.45 ); System.assertEquals(1, asyncJobList.size()); System.assertEquals(true, mock.methodCalled); } }
Finally, the trigger that does the http callout:
trigger updatePricesProposal on Proposta__c (after update){ if (Trigger.isUpdate){ for (Proposta__c prop : Trigger.new){ List <Proposta__c> prop_list = new List <Proposta__c>(); prop_list.add(prop); String serialisedProp = JSON.serialize(prop_list); try{ Proposta__c old_prop = Trigger.oldMap.get(prop.Id); if(prop.Cat_logo_de_pre_os__c != null && old_prop.Tempo_Botao_Preco__c.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'') != prop.Tempo_Botao_Preco__c.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'') ){ GCPManageCustomersService gcpService = new GCPManageCustomersService(serialisedProp, 'update'); ID jobID = System.enqueueJob(gcpService); } }catch(Exception e){ } } } }
I appreciate any help you can provide.
Best regards,
Refer the below link have solution for similar kind of ask.
https://github.com/GoogleCloudPlatform/salesforce-cloud-functions-crm-tutorial/blob/master/salesforce/force-app/main/default/classes/GCPManageCustomersServiceTest.cls
If this helps, Please mark it as best answer.
Thanks!!
Thank you very much for your answer. I am already using this github repo as inspiration to implement the callout in the trigger.
I don't understand why the assertions in my code aren't working but, in this repo example, it works:
System.assertEquals(1, asyncJobList.size());
System.assertEquals(true, mock.methodCalled);
Thanks!