Getting Started

  • What is Trailhead
  • myTrailhead
  • Trailblazer Community
  • Career Growth

Learn

  • Trails
  • Trailmixes
  • Modules
  • Projects

Credentials

  • Superbadges
  • Certifications
  • Maintain Certifications

Salesforce

  • Salesforce.com
  • AppExchange
  • Developers
  • Help Portal
© 2026 Salesforce, Inc. All rights reserved. Privacy Terms of Use Trust Accessibility Cookie Preferences
SalesforceTrailhead
  • Today
Apex Testing

Create Test Data for Apex Tests

+500 Points~45 minsHands-On Challenge
Apex TestingCreate Test Data for Apex Tests

Org:Mindful Goat Playground

Topics

  • Concepts
  • Hands-On Challenge

Looking for help?

Explore Help ResourcesShare Your Feedback
Learning Objectives

After completing this unit, you'll be able to:

  • Create a test utility class.
  • Generate test data in tests.

Concepts

Tests can't see org data by default. A test data factory creates the records each test needs, so tests stay independent and repeatable.

Hands-On Challenge

  1. Create a public @isTest class RandomContactFactory.
  2. Add a static method generateRandomContacts(Integer count, String lastName) that returns contacts named Test 1, Test 2, and so on.
Check ChallengeDemo check
Previous unitTest Apex TriggersBack to badge Apex Testing