0

I'm trying to test my @future method, in which I'm performing Custom Object update, but when I'm asserting values, updated object field equals null. Could it be a problem with method scope? I'm adding objects in @testSetup method and then updating a field in future method, but I don't see updated record fields in test method.

public static List<Custom_Object__c> objects;

@testSetup static void setup() {
    objects = new List<Custom_Object__c>();
    for (Integer i = 0; i < objectAmount; ++i) {
        objects.add(new Custom_Object__c(Hosting_Url__c = 'http://google.com'));
    }
    insert objects;
}

Test:

@isTest static void testOk() {
    setup();
    Test.startTest();

    Test.setMock(HttpCalloutMock.class, new UrlStatusHttpMock());
    UrlCheck.checkAndUpdate(JSON.serialize(objects));
    Test.stopTest();        

    for (Integer i = 0; i < objects.size(); ++i) {
        System.assertEquals('Living', objects.get(i).Url_Status__c, 'Wrong Url Status');
    }
}

And tested method:

@future(callout=true)
public static void checkAndUpdate(String objects) {
    List<Custom_Object__c> objectsToUpdate = new List<Custom_Object__c>();
    List<Custom_Object__c> objects = (List<Custom_Object__c>)JSON.deserialize(objects, List<Custom_Object__c>.class);

    for (Custom_Object__c object : objects) {            
            //some operations...
            object.Url_Status__c = 'Living';   
            objectsToUpdate.add(object);
        }
    }
    update objectsToUpdate;
}
0

You need to re-query for the records to find their status in the database.

List<MyObject__c> updatedRecords = [SELECT MyField__c FROM MyObject__c];
system.assertEquals(initialCount, updatedRecords.size(), 'All records should remain in the database');
for (MyObject__c record : updatedRecords)
{
    // these records should be affected by your action
}

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.