Created
December 18, 2012 21:55
-
-
Save anonymous/4332406 to your computer and use it in GitHub Desktop.
Apex Unit Test Gists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public with sharing class WarehouseUnitTests { | |
public WarehouseUnitTests() {} | |
@isTest | |
static public void testRequiredFields() { | |
//assertion: Merchandise requires Price and Quantity | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static public Boolean createBaselineData() { | |
Merchandise__c m = new Merchandise__c(Name='Rack Server',Price__c=1245.99,Quantity__c=500); | |
insert m; | |
Invoice__c i = new Invoice__c(); | |
insert i; | |
Line_Item__c li = new Line_Item__c(Name='1',Quantity__c=10,Merchandise__c=m.Id,Invoice__c=i.Id); | |
insert li; | |
return true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.debug('REQUIRED FIELDS:'); | |
//Detect all the required fields and debug out the result. This will help if a required field is added in the future. | |
Map<String, Schema.SObjectField> describeFields = Schema.SObjectType.Merchandise__c.fields.getMap(); | |
Map<String, Schema.DisplayType> fieldsTypes = new Map<String, Schema.DisplayType>(); | |
for(String field : describeFields.keyset()){ | |
Schema.DescribeFieldResult dr = describeFields.get(field).getDescribe(); | |
if(dr.isCreateable() && !dr.isNillable() && !dr.isDefaultedOnCreate()) { | |
System.debug('REQUIRED FIELD:' +field); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static public Boolean enterDataFromText(String dataString) { | |
List<String> data = dataString.split('\n'); | |
List<Merchandise__c> merch= new List<Merchandise__c>(); | |
for(String s : data) { | |
List<String> objectData = s.split(','); | |
Merchandise__c m = new Merchandise__c(Name=objectData[0].replaceAll('"',''),Quantity__c=Decimal.valueOf(objectData[1]),Price__c=Decimal.valueOf(objectData[2])); | |
merch.add(m); | |
} | |
try { | |
insert merch; | |
return true; | |
} catch(DMLException d) { | |
return false; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
global class MockHttpResponseGenerator implements HttpCalloutMock { | |
// Implement this interface method | |
global HTTPResponse respond(HTTPRequest req) { | |
// Optionally, only send a mock response for a specific endpoint | |
// and method. | |
System.assertEquals('http://api.salesforce.com/foo/bar', req.getEndpoint()); | |
System.assertEquals('GET', req.getMethod()); | |
// Create a fake response | |
HttpResponse res = new HttpResponse(); | |
res.setHeader('Content-Type', 'application/json'); | |
res.setBody('{"foo":"bar"}'); | |
res.setStatusCode(200); | |
return res; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//CreatedDate cannot be written, but it can be imported | |
List<Invoice__c> invoices = Test.loadData(Invoice__c.sObjectType, 'InvoiceData'); | |
update invoices; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
private class CalloutClassTest { | |
@isTest static void testCallout() { | |
// Set mock callout class | |
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator()); | |
// Call class that utilizes this endpoint | |
HttpResponse res = CalloutClass.getInfoFromExternalService(); | |
// Verify response received contains fake values | |
String contentType = res.getHeader('Content-Type'); | |
System.assert(contentType == 'application/json'); | |
String actualValue = res.getBody(); | |
String expectedValue = '{"foo":"bar"}'; | |
System.assertEquals(actualValue, expectedValue); | |
System.assertEquals(200, res.getStatusCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment