Code Monkey home page Code Monkey logo

apex-domainbuilder's Introduction

Apex Domain Builder Codacy Badge

Test Data Builder framework to setup test data for complex Apex integration tests in a concise, readable and flexible way.

TL;DR; Watch my a Youtube Code Live with Salesforce about the apex-domainbuilder

Deploy to Salesforce

Setting up test data for complex Apex integration tests is not easy, because you need to..:

  • set required fields even if irrelevant for the test
  • insert the objects in the right order
  • create relationships by setting Lookup fields
  • put ugly __c all over the place
  • clutter your code with Map<Id, SObject> to keep track of related records
  • reduce the DML statements to not hit Governor Limits

TestFactories as used by many developers and recommended by Salesforce.com can help to minimize ugly setup code by moving it to seperate classes. But over time those classes tend to accumulate complexity and redundant spaghetti code.

In the world of Enterprise software outside of Salesforce.com there are experts that have created patterns for flexible and readable (fluent, concise) test data generation. Among them, the most notable is Nat Pryce who wrote a great book about testing and somewhat invented the Test Data Builder pattern. It's based on the more general Builder Pattern which simplifies the construction of objects by moving variations from complex constructors to separate objects that only construct parts of the whole.

apex-domainbuilder brings those ideas to Apex testing:

  1. By incorporating a simple small Builder class for each test-relevant Domain SObject we centralize all the creation knowledge and eliminate redundancy.
@IsTest
public class Account_t extends DomainBuilder {

    // Construct the object

    public Account_t() {
        super(Account.SObjectType);

        name('Acme Corp');
    }

    // Set Properties

    public Account_t name(String value) {
        return (Account_t) set(Account.Name, value);
    }

    // Relate it to other Domain Objects 

    public Account_t add(Opportunity_t opp) {
        return (Account_t) opp.setParent(Opportunity.AccountId, this);
    }

    public Account_t add(Contact_t con) {
        return (Account_t) con.setParent(Contact.AccountId, this);
    }
}
  1. By internally leveraging the fflib_SObjectUnitOfWork for the DML all tests run dramatically faster.

  2. The Fluent Interface style of the Builder pattern combined with having all the database wiring encapsulated in the Unit of work made each test much more understandable.

    @IsTest
    private static void easyTestDataCreation() {

        // Setup
        Contact_t jack = new Contact_t().first('Jack').last('Harris');h

        new Account_t()
                .name('Acme Corp')
                .add( new Opportunity_t()
                                .amount(1000)
                                .closes(2019, 12)
                                .contact(jack))
                .persist();
        
        // Exercise
        ...
	
	
	// Verify
	...
    }
  1. Using Graph algorithms to autodetect the correct insert order in the Unit Of Work.
  2. Is able to handle self-reference fields (e.g. Manager Contact Lookup on Contact) by using a patched fflib Unit of Work.

apex-domainbuilder's People

Contributors

anmolgkv avatar anmollogicline avatar imjohnmdaniel avatar manjot0074 avatar rsoesemann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

apex-domainbuilder's Issues

How to handle circular relationship?

I'd like to use Domain Builder with circular relationships.

Object ParentObject__c with has a lookup to ChildObject__c
Object ChildObject__c has a M/D relation with ParentObject__c

@IsTest
public class Parent_t extends DomainBuilder {

    public Parent_t() {
        super(ParentObject__c.SObjectType);
    }
}
@IsTest
public class Child_t extends DomainBuilder{
	
    public Child_t(Parent_t p) {
        super(ChildObject__c.SObjectType);
        setParent(ChildObject__c.Parent__c, p);
    }
    
	public Child_t add(Parent_t p) {
        return (Child_t) p.setParent(ParentObject__c.Child__c, this);
    } 
}
@IsTest
private class TestDomainBuilder {
    
    @TestSetup
    private static void setup() {
    	Parent_t p = new Parent_t();
	Child_t c = new Child_t(p);
        //p.persist(); // this does not help as workaround
        c.add(p);
        p.persist();
    }
    
    @IsTest
    private static void testSetup() {
        System.assertEquals(1, [SELECT Id FROM ParentObject__c].size());
        System.assertEquals(1, [SELECT Id FROM ChildObject__c].size());      
	System.assertEquals(1, [SELECT Id FROM ParentObject__c WHERE Child__c != NULL].size());      
    }
}

Test will fail:

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent__c]: [Parent__c]

If I persist before adding the circular reference, the error is gone but the assert fails.

Failing test

When copied the library to my org and deployed it on scratch org for validation, one test is failing

DomainBuilder_Test.noUnneededRecords Fail System.AssertException: Assertion Failed: Expected: 6, Actual: 5
Class.DomainBuilder_Test.noUnneededRecords: line 19, column 1

[Question] remove uow dependency?

Good stuff Robert as always!

It is lovely that uow speeds DML actions up, but for many of us, bringing in parts of fflib into production code needs approval.

Also the main goal of this repo is to have a clean testBuilder, while uow is only nice-to-have feature. Wonder if it is better to decouple uow out? My 5cents.

Br,
Xi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.