3

We are facing a very strange issue when writing proper unit test using asserts.

We have two objects which have logical link with a field. So, we wrote a batch class to relate those two objects using a newly created lookup field. Batch is working fine and tested in all possible ways.

Problem starts here, when we write test class and run the batch in test class (created set up data and Test.startTest() etc.), we are getting that lookup field null in Test assert. But in main class system debug the lookup field is filled correctly. Even running the batch class through test class we are getting correct values in debug. But in Test asserts, it is NULL.

Data is created as:

    for(integer i=0; i<5; i++)
    {
        AccId__c aSID = new AcId__c();
        aSID.name = 'SID'+i;
        aSID.OwnerUser_SID__c = '1234567'+i;
        aSID.OwnerUser_Email_Address__c = 'cont'+i+'@xyz.com';
        aSid.OwnerUser_SID__c = 'ousidlead'+i;  
        aSID.Lead__c = null;
        SIDlist.add(aSID);
    }    
    insert SIDlist;

    for(integer i=0; i<5; i++)
    {    
        Lead ld = new Lead();
        ld.firstname = 'AngryLead';            
        ld.lastname = 'Cont'+i;            
        ld.title = 'CEO';
        ld.Email= 'cont'+i+'@xyz.com';
        ld.company = 'Salesforce';
        leadList.add(ld);
    }
    insert leadList;




   //This is how we are asserting:-



System.debug(' @@===> Total Ids '+acidIds);
    test.startTest();
        BatchLinkLeadToAcID bat = new BatchLinkLeadToAcID(); 
        database.executebatch(bat);

        System.schedule('Sync Leads - Hourly', '0 0 * * * ?', new BatchLinkLeadToAcID());
        List<Lead> leadListQueriedIn = [select id,Email,OwnerUser_SID__c from Lead where id in:leadIds];

        System.debug(' in Context Lead ==> '+leadListQueriedIn);
        list<AcID__c > newSIDlistIn = [select id,OwnerUser_SID__c,Lead__c, OwnerUser_Email_Address__c from AcID__c where id IN: acidIds];
        System.debug(' In Context Sid ==> '+newSIDlistIn);

    test.stopTest();

    List<Lead> leadListQueried = [select id,Email,OwnerUser_SID__c from Lead where id in:leadIds];
    System.debug(' Out of Context Lead ==> '+leadListQueried);

    System.debug(' >>=====> Access '+AcID__c.Lead__c.getDescribe().isAccessible());
    System.debug(' >>=====> Access '+AcID__c.Lead__c.getDescribe());

    List<AcID__c > newSIDlist = [select id,OwnerUser_SID__c,Lead__c, OwnerUser_Email_Address__c from AcID__c where id IN: acidIds];
    System.debug(' Out of Context Sid ==> '+newSIDlist);

    for(integer i=0; i<5; i++)
    {
        system.assertequals(leadListQueried[i].OwnerUser_SID__c, newSIDlist[i].OwnerUser_SID__c ); // PASS
        system.assertequals(leadListQueried[i].id, newSIDlist[i].Lead__c ); // FAIL
        System.assert(newSIDlist[i].Lead__c != null); //FAIL
    }

We tested creating same records by UI as well. It is working. It is only failing in Test class. What can be the reason? Code run in production without any issue but above specific assert related to reference field are failing.

Any help would be highly appreciated.

23
  • how many records are in batch? as far as I know SF executes only first batch from batch job
    – kurunve
    Feb 17, 2016 at 15:04
  • Total 5 records @kurunve
    – Ashwani
    Feb 17, 2016 at 15:05
  • 3
    and Test.stopTest() is used as well?
    – kurunve
    Feb 17, 2016 at 15:07
  • Those asserts can only occur after Test.stopTest(). If that's what you're doing, have you tried changing aSID.Lead__c = null; to aSID.Lead__c.isEmpty(); or just omitting it entirely?
    – crmprogdev
    Feb 17, 2016 at 15:29
  • Of course! Test.stopTest() is there :D. @kurunve aSID.Lead__c.isEmpty() must have Id actually it should not be empty or null.
    – Ashwani
    Feb 17, 2016 at 15:34

0

You must log in to answer this question.

Browse other questions tagged .