Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Proper Unit Test Structure in Apex
public class ExampleController
public Integer differenceInFooWidgetsAndStubCount(Id fooId, Id stubId){
Foo__c foo = [
SELECT
Id,
(SELECT Id FROM Widget__r)
FROM
Foo__c
WHERE
Id = :fooId
];
Stub__c stub = [
SELECT
Id, Count_One__c, Count_Two__c, Use_Count_One__c
FROM
Stub__c
WHERE
Id = :stubId
];
if(stub.Use_Count_One__c){
return foo.Widget__r.size() - stub.Count_One__c;
}else{
return foo.Widget__r.size() - stub.Count_Two__c;
}
}
}
@isTest
public class ExampleControllerTest{
static testMethod void testDifferenceInFooWidgetsAndStubCountOne(){
Map<String, List<sObject>> objectMapping = TestUtilities.generateTestModel(true);
Foo__c foo = objectMapping.get('Foo__c').get(0);
Stub__c stub = objectMapping.get('Stub__c').get(0);
Test.startTest();
ExampleController controller = new ExampleController();
Integer difference = controller.differenceInFooWidgetsAndStubCount(foo.Id, stub.Id);
Test.stopTest();
System.assertEquals(2, difference, 'The difference should be 2 when Count_One__c is used');
}
static testMethod void testDifferenceInFooWidgetsAndStubCountTwo(){
Map<String, List<sObject>> objectMapping = TestUtilities.generateTestModel(false);
Foo__c foo = objectMapping.get('Foo__c').get(0);
Stub__c stub = objectMapping.get('Stub__c').get(0);
Test.startTest();
ExampleController controller = new ExampleController();
Integer difference = controller.differenceInFooWidgetsAndStubCount(foo.Id, stub.Id);
Test.stopTest();
System.assertEquals(1, difference, 'The difference should be 1 when Count_Two__c is used');
}
}
@isTest
public class TestUtilities{
public static Foo__c generateFoos(Integer count){
List<Foo__c> foos = new List<Foo__c>();
for(Integer i = 0; i<count; i++){
foos.add(new Foo());
}
return foos;
}
public static List<Widget__c> generateWidgets(List<Foo__c> foos, Integer count){
List<Widget__c> widgets = new List<Widget__c>();
for(Foo__c foo:foos){
for(Integer i = 0; i<count; i++){
widgets.add(new Widget(
Foo__c = foo.Id,
Foo__r = foo
));
}
}
return widgets;
}
public static List<Stub__c> generateStubs(Boolean useCountOne, Integer count){
List<Stub__c> stubs = new List<Stub__c>();
for(Integer i = 0; i<count; i++){
stubs.add(new Stub__c(
Count_One__c = 1,
Count_Two__c = 2,
Use_Count_One__c = useCountOne
));
}
return stubs;
}
public static Map<String, List<sObject>> generateTestModel(Boolean useCountOne){
List<sObject> objectsToAdd = new List<sObject>();
List<Foo__c> foos = generateFoos(1);
List<Stub__c> stubs = generateStubs(useCountOne, 1);
objectsToAdd.add(foos);
objectsToAdd.add(stubs);
insert objectsToAdd;
objectsToAdd.clear();
List<Widget__c> widgets = generateWidgets(foos.get(0), 3);
objectsToAdd.add(widgets);
insert objectsToAdd;
objectsToAdd.clear();
Map<String, List<sObject>> objectMapping = new Map<String, List<sObject>>();
objectMapping.put('Foo__c', foos);
objectMapping.put('Stub__c', stubs);
objectMapping.put('Widget__c', widgets);
return objectMapping;
}
}
Owner

jessealtman commented Apr 19, 2014

This code is from my blog article Proper Unit Test Structure in Apex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment