Code Monkey home page Code Monkey logo

testsobjectbuilder's Introduction

Test SObject Builder for Salesforce Apex

Deploy to Salesforce

Overview

This is an freestanding version of the Nebula framework's Test SObject Builder, used to generate test records for unit tests - it has been updated to remove any dependencies on the rest of the Nebula framework.

Implementation

TestSObjectBuilder.cls is the primary focus of this project - it implements the interface ITestSObjectBuilder. This provides a framework for definining classes that can be used for test data generation.

TestSObjectBuilder is defined as a test class - test classes cannot be abstract, so the subclass, TestSObjectBuilder.Base, has been created. This allows TestSObjectBuilder to be marked as @isTest while still providing reusable code via an abstract class.

  1. To leverage TestSObjectBuilder, create a class that extends TestSObjectBuilder.Base. The recommended approach is to create 1 class per SObject type, with 1 or more subclasses (depending on your needs). For example, you might have
    1. Class: TestAccountBuilder - 1 outer class that handles creating any type of test data for the Account object
      1. Subclass: Base - a very basic the most simple way to insert a test record
      2. Subclass: RecordTypeX - another subclass that creates an account with a specific record type
  2. For each subclass that you create to extend TestSObjectBuilder.Base, there are 2 methods that you must override
    1. getSObjectType() - defines what SObject type is being created
    2. getDefaultFieldsAndValues - defines what fields need to be set in order to create a test record. This will be specific to your org, based on any custom fields, validation rules, etc that have been added.
  3. When you need to create a test account, call the build method on your class.
    Account myTestAccount = TestAccountBuilder.build();
    

Samples

2 sample classes have been included in the repo

  • Sample_TestAccountBuilder.cls - demonstrates the most basic implementation of TestSObjectBuilder.Base
  • Sample_TestContactBuilder.cls - demonstrates how to make additional fields, like contact.AccountId, required to be populated by adding a new constructor with an account ID parameter

testsobjectbuilder's People

Contributors

jongpie avatar

Watchers

James Cloos avatar

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.