Accounting Apex API Developer Reference
Writing Apex Tests

Writing Apex Tests

When developing against the API you will likely be doing one or more of the following types of API integrations:

  • Use Case 1: Calling an Accounting API directly to perform a task
  • Use Case 2: Calling a Salesforce DML API directly to perform a record operation
  • Use Case 3: Implementing a custom Apex Trigger

This code sample illustrates how to setup a basic environment within an Apex Test, to allow this type of code to be covered and tested as per the platform requirements and best practices.

Code Sample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.
 
@IsTest
private class CreateTransactionTest
{
    @IsTest
    private static void createTransaction()
    {
        // Setup a test Company
        System.runAs([select Id from User where Id = :UserInfo.getUserId()][0])
        {
            Test.startTest();
            c2g__codaCompany__c company = c2g.CODAAPICompany_2_0.CreateTestCompany('ApexTestCompany', null, null);
            Test.stopTest();
        }
        // Company should be made current as only one
        System.assertEquals('ApexTestCompany', c2g.CODAAPICompany_2_0.GetCurrentCompanies(null)[0]);
 
        // Create General Ledger Account for transactions
        c2g__codaGeneralLedgerAccount__c gla = new c2g__codaGeneralLedgerAccount__c();
        gla.Name = 'Brochures';
        gla.c2g__ReportingCode__c = 'xyz1';
        gla.c2g__Type__c = 'Balance Sheet';
        insert gla;
 
        // Post a Journal to create the transactions
        c2g__codaJournal__c journal = new c2g__codaJournal__c();
        journal.c2g__JournalDate__c = Date.today();
        insert journal;
 
        c2g__codaJournalLineItem__c journalLine1 = new c2g__codaJournalLineItem__c();
        journalLine1.c2g__LineType__c = 'General Ledger Account';
        journalLine1.c2g__Journal__c = journal.Id;
        journalLine1.c2g__GeneralLedgerAccount__c = gla.Id;
        journalLine1.c2g__Value__c = 42;
        c2g__codaJournalLineItem__c journalLine2 = new c2g__codaJournalLineItem__c();
        journalLine2.c2g__LineType__c = 'General Ledger Account';
        journalLine2.c2g__Journal__c = journal.Id;
        journalLine2.c2g__GeneralLedgerAccount__c = gla.Id;
        journalLine2.c2g__Value__c = -42;
        insert new List<c2g__codaJournalLineItem__c> {journalLine1, journalLine2};
 
        c2g.CODAAPIJournal_12_0.PostJournal(null, c2g.CODAAPICommon.getRef(journal.Id, null));
 
        // Assert transaction exists
        System.assertEquals(0, [select c2g__DocumentTotal__c from c2g__codaTransaction__c LIMIT 1][0].c2g__DocumentTotal__c);
    }
}
© Copyright 2009–2023 Certinia Inc. All rights reserved. Various trademarks held by their respective owners.