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
Jyosi jyosiJyosi jyosi 

Too Many Callout in Test Class System.LimitException: Too many callouts: 101

Hello Every one,

When ever i run the Test class i get the following Error "System.LimitException: Too many callouts: 101"
It happens only in test Class ,but when i run the job anomusly it will handle 400 records.

I am  struck badly can you please help me out


@isTest(Seealldata=true)
private class GlobalComplaintWebSvcCalloutTest {
   
    @isTest static   void testEchoString() {  
       String batchID;
       
         string ReadyForSAP= 'Ready For SAP';
        GlobalComplaintOutbound outbound= new GlobalComplaintOutbound();
       
         Test.setMock(WebServiceMock.class, new GlobalComplaintOutbountTest());
      // Test.startTest();
      hiGcsQncreateQncreateresponse.Response_element res= new  hiGcsQncreateQncreateresponse.Response_element();
       
       List<Case> scope = new List<Case>();
       List<case> caseQuery= new List<Case>();
      
       
       case CaseList= new Case();
       
      Database.BatchableContext BC;
         GLIntegrationAuthenticationSettings__c GLIntegration = GLIntegrationAuthenticationSettings__c.getAll().get('BizTalk');
      outbound.start(BC);
       outbound.execute(BC, scope);

Mock Class

global class GlobalComplaintOutbountTest implements WebServiceMock {
  
    global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
                 wwwHollisterComGcsQncreate.BasicHttpBinding_ITwoWayAsync respElement= new wwwHollisterComGcsQncreate.BasicHttpBinding_ITwoWayAsync();
       hiGcsQncreateQncreaterequest.QN_element Request1= new hiGcsQncreateQncreaterequest.QN_element();
        GLIntegrationAuthenticationSettings__c GLIntegration = GLIntegrationAuthenticationSettings__c.getAll().get('BizTalk');
        hiGcsQncreateQncreateresponse.QNCreateResponse_element res = new hiGcsQncreateQncreateresponse.QNCreateResponse_element();
        hiGcsQncreateQncreateresponse.Response_element response_Element= new  hiGcsQncreateQncreateresponse.Response_element();
        hiGcsQncreateQncreaterequest.complainantInfo_element complainantInfo_element= new hiGcsQncreateQncreaterequest.complainantInfo_element();
        hiGcsQncreateQncreaterequest.complaintInfo_element  complaintInfo_element= new hiGcsQncreateQncreaterequest.complaintInfo_element();
        hiGcsQncreateQncreaterequest.productInfo_element productInfo_element= new hiGcsQncreateQncreaterequest.productInfo_element();
        hiGcsQncreateQncreaterequest.QN_element  QN_Element= new hiGcsQncreateQncreaterequest.QN_element();
        List<hiGcsQncreateQncreaterequest.QN_element> QN_List= new List<hiGcsQncreateQncreaterequest.QN_element>();
        hiGcsQncreateQncreaterequest.qnInfo_element qnInfo_element= new hiGcsQncreateQncreaterequest.qnInfo_element();
        hiGcsQncreateQncreaterequest.QNSet_element QNSet_element= new hiGcsQncreateQncreaterequest.QNSet_element();
        hiGcsQncreateQncreateresponse.QNCreateResponse_element c= new  hiGcsQncreateQncreateresponse.QNCreateResponse_element();
      
               
        // wwwHollisterComGcsQncreate
        wwwHollisterComGcsQncreate.BasicHttpBinding_ITwoWayAsync BasicHttpBinding_ITwoWayAsync = new wwwHollisterComGcsQncreate.BasicHttpBinding_ITwoWayAsync();
        QN_Element.qnInfo=qnInfo_element;
         QN_Element.productInfo=productInfo_element;
         QN_Element.complaintInfo=complaintInfo_element;
         QN_List.add(QN_Element);
         QNSet_element.QN=QN_List;

        //BasicHttpBinding_ITwoWayAsync.opQNCreate(QN_List);
        respElement.endpoint_x = 'https://diid.hollister.com/QNCreateWcfService/QNCreateService.svc';
        hiGcsQncreateQncreateresponse.Response_element response_x= respElement.opQNCreate(QN_List);
         
        
             if(response_x!=null)
             {
                   Database.executeBatch(new GlobalComplaintOutbound(), 1);
           response.put('response_x', response_x);
                 }
             
               
           }
               
   }
thanks for help in advance 

Regards,
Jyo

 
Edward EncarnacionEdward Encarnacion
Batch classes will try to basically execute all batches in one go so since your test class is set to see all data  = true, The test class will try to run all the outbound calls in one execution thread. 

Best practice in test batch apex would be to create the test cases in the test class and set see all data to false. Con of that would mean you will create the custom setting record in the test class itself ass well
Jyosi jyosiJyosi jyosi
Thank you but when i make Seealldata=false    Test.setMock(WebServiceMock.class, new GlobalComplaintOutbountTest()); is not getting covered.
How can i set the username and password which i am getting from custom setting.
Can you please guide me on this?

Thanks

Regards,

jyo
@isTest(Seealldata=false)
private class GlobalComplaintWebSvcCalloutTest {
   
     static   testMethod void testEchoString() {  
       String batchID;
        
        Contact Test_Contact= new Contact(LastName='ABC',firstName='ABC',Status__c='ABC',Sample_Shipping_City__c='ABC',Sample_Shipping_Street__c='ABC',
                                        Sample_Shipping_Zip_Postal_Code__c='60543',Sample_Shipping_Country__c='US',Data_Protection_Authorization__c='US',Brand__c='ABC',
                                        Type_of_Consumer__c='ABC',
                                          Source__c='ABC',Source2__c='ABC',MailingCity='1231212',MailingState='121211878',MailingPostalCode='121121111111111',MailingCountry='ysu');
        insert Test_Contact;
        
         List<Case> scope = new List<Case>();
       
               Case Caseinsert1 = new Case(Origin='ABC',Complaint_Source__c='ABC',Voice_of_Customer__c='ABC',Entered_Product__c='ABC',
                                           GCS_Customer_Number__c='2500',GCS_Distribution_Channel__c='00'
                                   ,GCS_Divsion__c='00',GCS_Sales_Org__c='61',Country__c='DE',Normal_Usage__c='1',
                                     Actual_Usage__c='2',Credit__c=true,Problem_Damage_Code__c='1211',Samples_to_be_Sent__c='1212',ContactId=Test_Contact.Id,
                                           status='Ready For SAP');
        insert Caseinsert1;
        scope =[SELECT ID,Date_Received__c,GL_Complaint_Number__c,Complaint_Mailing_City__c,Complaint_Mailing_State_Provin__c,
     complaint_Mailing_Zip_Postal_Code__c,Complaint_Mailing_Country__c,
                Complaint_Phone__c,Complaint_Source__c,Origin,product__c,Complaint_Qty__c,Complaint_Qty_Unit_of_Measure__c,
                Complaint_Lot3__c,Complaint_Lot__c,Complaint_Lot_2__c,Complaint_Code__c,Complaint_Lot_Unknown__c,Voice_of_Customer__c,
                Associated_Product__c,Problem_Damage_Code__c,Complaint_Lot2__c,Normal_Usage__c,Actual_Usage__c,Send_Samples__c,GCS_Distribution_Channel__c,Description,
                GCS_Divsion__c, GCS_Sales_Org__c,Product_Brand__c,Cust_Number__c,Complaint_Mailing_Street__c,Samples_to_be_Sent__c,Contact.Email,
                Potential_Injury__c,Product__r.Name,Account.Name,Contact_First_Name__c,Opt_out_complaint_code__c,Go_to_Potential_Reason__c,Contact.LastName,Contact_Last_Name__c,GCS_Customer_Number__c,CreatedBy.Name,LastModifiedBy.Name
                 from case where status='Ready For SAP'  and Id=:Caseinsert1.Id]; 
        
        GlobalComplaintOutbound outbound= new GlobalComplaintOutbound();
       //Test.startTest();
         Test.setMock(WebServiceMock.class, new GlobalComplaintOutbountTest());
      // Test.startTest();
      hiGcsQncreateQncreateresponse.Response_element res= new  hiGcsQncreateQncreateresponse.Response_element();
       
          
      Database.BatchableContext BC;
         GLIntegrationAuthenticationSettings__c GLIntegration = GLIntegrationAuthenticationSettings__c.getAll().get('BizTalk');
      //String UserName='Test';
     //String Password='121';
      outbound.start(BC);
        for(Case CaseList:Scope)
        {
               outbound.execute(BC, scope);
        }
AB TestAB Test
Hi 

 keeping seeAllData= false is the ideal way the test class should be .
Now you are facing issue that code is not getting covered as  username and password which is setting  from custom setting is not set.
So you can insert custom setting with the same name in you test class before invoication of constructor or method.
You can insert custom setting in the same way as you insert any other SF object

hope it helps.
Please like or mark the answer as best answer if it helps