I'm trying to write most of my tests without hitting salesforce database. For that I'm implementing some simple mock class where I just return some SOQL queries when needed. The problem I'm facing now is with AggregateResult. Was someone able to create in memory this so that I can returned it in one of my mocked methods?

share|improve this question
    
Kinda depends on what you are trying to do with them. – Adrian Larson 3 hours ago
    
I'm just interested in why you would want to avoid hitting the database in your test. – rael_kid 3 hours ago
    
@rael_kid I've mocked all sorts of records, simply because it's a great way to unit test individual methods instead of end-to-end tests. – sfdcfox 2 hours ago

You can use serialization if you're loathe to query:

AggregateResult agg = (AggregateResult)JSON.deserialize(
    '{"sumAlias": 1, "otherAlias": "Some Text"}', AggregateResult.class
);

You can simplify generating the payload using some sort of proxy class:

class Proxy
{
    Integer sumAlias;
    String otherAlias;
}
Proxy aggregateProxy = new Proxy();
aggregateProxy.sumAlias = 1;
aggregateProxy.otherAlias = 'Some Text';

AggregateResult agg = (AggregateResult)JSON.deserialize(
    JSON.serialize(aggregateProxy), AggregateResult.class
);
share|improve this answer
1  
can you please check how it will work this line Proxy aggregateProxy = new AggregateProxy(); is it typo error? – Santanu Boral 1 hour ago
    
Right that's what I get for typing it out manually. – Adrian Larson 1 hour ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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