[SalesForce] Apex Test Class Assert for Void

How can I build an Apex test class to validate a class method that has a @future HTTPRequest setting? The @future method returns a void, but I can't test a void value with a System.assert[Not]Equals

The change to the system that triggers the future async call happens on user interaction, so any changes to the system would already be set. The scenario is that the user changes an sObject field (Account Owner) and that change triggers the async http request:

trigger AccountOwnerUpdate on Account (after update) {
    for (Account a : Trigger.new){
        if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
            List<Account> aId = [Select OwnerId From Account where Id = :a.Id];
            List<User> oName = [Select Name from User where Id = :aId[0].OwnerId];

            String newOwnerName = EncodingUtil.urlEncode(oName[0].Name, 'UTF-8');
            String accountId = a.Id;

            AccountOwnerUpdateTest.remoteUpdateAccountOwner(accountId, newOwnerName);
        }
    }
}


global class AccountOwnerUpdateTest {
     @future(callout = true)
     public static void remoteUpdateAccountOwner(String accountId, String newOwnerName) {

        HttpRequest req = new HttpRequest();
        String endpoint = '[My url]/'+accountId+'/[param]/'+newOwnerName;

        req.setEndpoint(endpoint);
        req.setMethod('GET');

        Http http = new Http();
        HTTPResponse res = http.send(req);

   }
}

@isTest (seeAllData=true)
public class AccountOwnerUpdateTestClass {
    static testMethod void validateOwnerUpdate() {
        Test.startTest();
        Test.setMock(HttpCalloutMock.class, new MockHttpRequestResponseGenerator());
        AccountOwnerUpdateTest.remoteUpdateAccountOwner('id','owner');
        Test.stopTest();
// System.assertEquals([?])
    }
}

Best Answer

Your use case is one where a future method is called that does a callout.

How do you assert that this happened?

Immediately before AccountOwnerUpdateTest.remoteUpdateAccountOwner('id','owner'); do: Integer calloutsBefore = Limits.getCallouts();

After Test.stopTest() (as the async executuon won't occur until this statement is reached)

System.assertEquals(calloutsBefore+batchsizebeingtested,Limits.getCallouts());

How do you assert the callout worked?

This is more problematic because you have to mock the results so you really can't test the actual remote Http service did what you expect. hence, to address this would be done via integration testing using live users, or Selenium-type tools.