0

So I've had gained a lot of experience coding apex classes and Visualforce pages but there is still one concept that is really causing frustration for me and that is how to write proper test classes.

I'm specifically having an issue with a class I'm writing in which its sole purpose will be to create test data for future test classes. I'm having trouble understanding how to get the test data to relate together.

Below is the class and test class I wrote with very simple methods. One method that simply creates a bare account and inserts it + returns it. A second method that 1.) uses the first method to create and account and 2). creates a bare custom record called an Audit, relates it to the test account that was just created, inserts it, and returns it.

The issue I'm having is that whenever I run the below test my assertion fails to show that the audit ever got related to the account. Even weirder, it only fails if I'm using the audit that was returned! It will pass fine if I re-query for that audit and use that instead. Can someone explain to me why this happens? Is there a better way I should be doing this?

Apex Class

global class testData {

//Creates a basic test account, record type of client. Use if only an account is needed.
public static Account createTestAccount(){

    //Create a test account, insert it, and also return in for use in reference later.
    Account testAccount = new account(
        Name = 'Test Account',
        RecordTypeId = '012C0000000GCJv', 
        Industry = 'Banking',
        Status__c = 'Active'
    );

    insert testAccount;
    return testAccount;
}

//Creates an audit and relates it to a test account. An account will be
//automatically created so it is not necessary to run the createTestAccount() method.
public static Location_Audits__c createTestAudit(){
    Account testAccount = testData.createTestAccount();

    //Generate an audit, insert it, and then return it.
    Location_Audits__c testAudit = new Location_Audits__c(
        Account__c = testAccount.Id, 
        Stage__c = 'Gathering Docs From Client', 
        Audit_Type__c = 'CO2', 
        Current_Analyst__c = 'Andrew Bettke',
        Revisit_Notes__c = testAccount.Id
    );

    insert testAudit;
    return testAudit;
}
}

Test Class

@isTest
private class testDataTest {

//Create a test account.
static testMethod void testCreateTestAccount() {
    Account testAccount = testData.createTestAccount();
    System.assert(testAccount != null);
    System.assert(testAccount.Id != null);
    System.assert(testAccount.Name == 'Test Account');
}

//Create a test audit and relate it to a test account.
static testMethod void testCreateTestAudit() {

    //Using the returned audit from the class method fails the assertion.
    Location_Audits__c testAudit = testData.createTestAudit();

    //When I uncomment this section and use the queried audit it works fine.
    //Account testAccount = [SELECT Id, Name FROM Account WHERE Name = 'Test Account'];
    //testAudit = [SELECT Id, Name, Account__r.Name FROM Location_Audits__c WHERE Account__r.Id = :testAccount.Id];

    System.debug(testAudit.Name);
    System.debug(testAudit.Account__r.Name);
    System.assert(testAudit != null);
    System.assert(testAudit.Account__r.Name == 'Test Account');
}
}
2

Your problem is this line:

   System.assert(testAudit.Account__r.Name == 'Test Account');

The relationship is not populated when you assign the ID to the record on creation.

Only the explicit values you set plus the resulting ID of the record are what will be present in a variable sObject that you create. Anything else will have to be queried for.

| improve this answer | |
  • Oh ok. So when I actually set Account__c = testAccount.Id, even after I insert that audit, the only thing that remains in the variable testAudit is that the account field has a string equal to the Id, it does not have a way of actually traversing up that that record. That seems like something the compiler should catch right? Thanks for the input! – Andrew Bettke Dec 3 '15 at 15:32
  • You are correct and it is a runtime thing not a compile time. On Compile your expression is valid.... – Eric Dec 3 '15 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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