1

I have the below code block in a batch method to send outbound MQ messages to the iSeries, which triggers an update of the record when the response is received.

try
{
    cif_Equation_Integration_Outbound_v1.OmgUpdate = false;
    OutboundMessageQueue__c dumbyId = [select id from OutboundMessageQueue__c limit 1];
    cif_Equation_Integration_Outbound_v1.Entity_Outbound(dumbyId.Id,c.id);

    string outMQFailureCode='';
    string outMQFailureReason='';

    if(cif_Equation_Integration_Outbound_v1.isSuccessfull == true){
        OutboundMessageQueue__c omq = new OutboundMessageQueue__c();
        omq.Message_Id__c = 'SPCIFA_BATCH';
        omq.MapId__c = '900';
        omq.sfObject__c = 'cif_Entity__c';
        omq.SFID__c = c.Id;
        omq.RequestType__c = 'CIF Equation Sync: ';
        omq.Description__c = 'Sync '+c.Name+' ('+c.ID_CIF__c+') with Equation'; 
        omq.Delivery_Failure_Code__c = cif_Equation_Integration_Outbound_v1.FailureCode;
        omq.Delivery_Failure_Reason__c = cif_Equation_Integration_Outbound_v1.FailureReason;

        outMQFailureCode = string.valueOf(omq.Delivery_Failure_Code__c);
        outMQFailureReason = omq.Delivery_Failure_Reason__c;

        insert omq;
        system.debug('MQ Msg created and inserted for: ' + c.Name);
    }

    updatedEntities += string.valueOf(c.Id)+','+ string.valueOf(c.Name)+',' +  string.valueOf(c.ID_CIF__c)+',' + myDatetimeStr + ',' + outMQFailureCode + ',' + outMQFailureReason + '\n';

    RecordNo++;        
    system.debug('EntityId: '+c.id);        
}
catch (exception ex){ ...

The batch runs fine, and does what it's supposed to, but I am having trouble getting code coverage of the section within the If(cif_Equation_Integration_Outbound_v1.isSuccessfull == true){ ..}

Here is my test method:

@isTest //(SeeAllData=true)
public class Batch_Ent_Update_Eqn_Status_TESTMETHOD{

    public static testmethod void testBatch_Ent_Update_Eqn_Status(){

        Service_Point_Setting__c sps = new Service_Point_Setting__c();
        sps.name = 'Migration';
        sps.value__c = 'false';
        insert sps;

        List <cif_Entity__c> listEntities = new List<cif_Entity__c>();

        cif_Entity__c ent = new cif_entity__c();
        ent.Salutation__c = 'Mrs';
        ent.Firstname__c    = 'Yellow';
        ent.Lastname__c = 'Sky';        
        ent.Equation_Status__c='Updating Equation';
        insert ent;
        listEntities.add(ent);

        test.startTest();
        string outMQFailureCode='';
        string outMQFailureReason='';

        cif_Equation_Integration_Outbound_v1.isSuccessfull=true;

        OutboundMessageQueue__c omq = new OutboundMessageQueue__c();
        omq.Message_Id__c = 'TEST_BATCH';
        omq.MapId__c = '900';
        omq.sfObject__c = 'cif_Entity__c';
        omq.SFID__c = ent.Id;
        omq.RequestType__c = 'Test Insert Message: ';
        omq.Description__c = 'TEST'; 
        omq.Delivery_Failure_Code__c = cif_Equation_Integration_Outbound_v1.FailureCode;
        omq.Delivery_Failure_Reason__c = cif_Equation_Integration_Outbound_v1.FailureReason;

        outMQFailureCode = string.valueOf(omq.Delivery_Failure_Code__c);
        outMQFailureReason = omq.Delivery_Failure_Reason__c;

        insert omq;

        BATCH_Ent_Update_Eqn_Status myClass = new BATCH_Ent_Update_Eqn_Status();
        myClass.lnumber = '1';
        database.executebatch(myClass,1);

        system.assertEquals(ent.Equation_Status__c='Equation Updated', ent.Equation_Status__c);
        system.debug('entity: ' + ' ' + ent.ID_CIF__c + '; ' + ent.Firstname__c + ', ' + ent.Lastname__c + ', Eqn Status: ' + ent.Equation_Status__c);
        /*
        for (cif_entity__c ent: listEntities){
            system.assertEquals(ent.Equation_Status__c='Equation Updated', ent.Equation_Status__c);
            system.debug('entity: ' + ' ' + ent.ID_CIF__c + '; ' + ent.Firstname__c + ', ' + ent.Lastname__c + ', Eqn Status: ' + ent.Equation_Status__c);
        }*/
        test.stopTest();                
    }
}

so as you an see, I have tried setting cif_Equation_Integration_Outbound_v1.isSuccessfull=true; in order to get that block to run, but no, here is what my code coverage looks like:

enter image description here

so what is the correct way to get that block of code to run?

We are trying to not use SeeAllData=true as we want the test methods to work automatically in every Org we deploy it to...

  • Correct way is try to avoid exception thrown inside Entity_Outbound method at line 56. Cn not say more from what code/screenshots – kurunve Apr 15 '16 at 9:38
  • @kurunve: yes you are correct in that, so in order to ensure there is an available id in the OutboundMessageQueue__c object I am inserting one record before starting the batch – Our Man in Bananas Apr 15 '16 at 9:41
3

The problem is in this line:

cif_Equation_Integration_Outbound_v1.Entity_Outbound(dumbyId.Id,c.id);

Something goes terribly wrong there and it throws an exception which is then caught by your catch block and handled. That's also why your catch block is covered and the rest of try is red. I'd recomment debugging cif_Equation_Integration_Outbound_v1.Entity_Outbound() method to find what goes wrong and why does it throw an exception

2

Add the commented line into the apex code below

cif_Equation_Integration_Outbound_v1.OmgUpdate = false;
OutboundMessageQueue__c dumbyId = [select id from OutboundMessageQueue__c limit 1];
cif_Equation_Integration_Outbound_v1.Entity_Outbound(dumbyId.Id,c.id);
String outMQFailureCode = '';
String outMQFailureReason = '';

// Add this line 
if(isRunningTest()) {
    cif_Equation_Integration_Outbound_v1.isSuccessfull = true;
}
  • ram r -- you really ought to format your code before posting as an answer – cropredy Apr 15 '16 at 17:34
  • Ok will format code going ahead - thanks @cropredy – ram r Apr 16 '16 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.