Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<a href="https://beyondthecloud.dev"><img alt="Beyond The Cloud logo" src="https://img.shields.io/badge/MADE_BY_BEYOND_THE_CLOUD-555?style=for-the-badge"></a>

<img alt="API version" src="https://img.shields.io/badge/api-v65.0-blue?style=for-the-badge">
<img alt="API version" src="https://img.shields.io/badge/api-v67.0-blue?style=for-the-badge">
<a href="https://github.com/beyond-the-cloud-dev/test-lib/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/badge/license-mit-green?style=for-the-badge"></a>
<img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/beyond-the-cloud-dev/test-lib?style=for-the-badge&logo=github&color=blue">
<img alt="GitHub Release" src="https://img.shields.io/github/v/release/beyond-the-cloud-dev/test-lib?display_name=tag&style=for-the-badge&color=blue">
Expand Down
2 changes: 1 addition & 1 deletion examples/main/default/classes/AccountTestModule.cls
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class AccountTestModule implements TestModule.BuilderProvider, TestModule
}
}

private class Mocker extends TestModule.RecordMocker {
public class Mocker extends TestModule.RecordMocker {
private Mocker() {
super(new Account(Name = 'Test Account', Industry = 'Technology'));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<apiVersion>67.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion examples/main/default/classes/ContactTestModule.cls
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ContactTestModule implements TestModule.BuilderProvider, TestModule
}
}

private class Mocker extends TestModule.RecordMocker {
public class Mocker extends TestModule.RecordMocker {
private Mocker() {
super(new Contact(FirstName = 'Test', LastName = 'Contact', Email = 'test.contact@example.com'));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<apiVersion>67.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion examples/main/default/classes/OpportunityTestModule.cls
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class OpportunityTestModule implements TestModule.BuilderProvider, TestMo
}
}

private class Mocker extends TestModule.RecordMocker {
public class Mocker extends TestModule.RecordMocker {
private Mocker() {
super(new Opportunity(Name = 'Test Opportunity', StageName = 'Prospecting', CloseDate = Date.today().addDays(30)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<apiVersion>67.0</apiVersion>
<status>Active</status>
</ApexClass>
44 changes: 33 additions & 11 deletions force-app/main/default/classes/TestModule.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2026 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
* Licensed under the MIT License (https://github.com/beyond-the-cloud-dev/test-lib/blob/main/LICENSE)
*
* v0.1.0
* v0.2.0
**/
@IsTest
@TestVisible
Expand Down Expand Up @@ -68,17 +68,23 @@ private class TestModule {
return new ListRandomizer(values);
}

private static void validateRecordsAmount(Integer amount) {
if (amount == null || amount < 0) {
throw new TestModuleException('Amount must be zero or greater, but was: ' + amount + '.');
}
}

public abstract class RecordBuilder implements Builder {
private SObject prototype;
private Template templates;
private RandomizersContainer randomizers = new RandomizersContainer();

public RecordBuilder(SObject prototype) {
this.prototype = prototype;
this.prototype = prototype.clone(true, true, true, true);
}

public RecordBuilder(Template templates) {
this.prototype = templates.defaultTemplate();
this(templates.defaultTemplate());
this.templates = templates;
}

Expand Down Expand Up @@ -126,17 +132,21 @@ private class TestModule {
}

public SObject build() {
this.randomizers.randomizeFieldsValues(this.prototype, 0);
return this.prototype;
SObject record = this.prototype.clone(true, true, true, true);
this.randomizers.randomizeFieldsValues(record, 0);
return record;
}

public SObject buildAndInsert() {
insert as system this.prototype;
return this.prototype;
SObject record = this.build();
insert as system record;
return record;
}

public List<SObject> build(Integer amount) {
List<SObject> records = new List<SObject>();
validateRecordsAmount(amount);

List<SObject> records = (List<SObject>) Type.forName('List<' + this.prototype.getSObjectType() + '>').newInstance();
this.randomizeFieldsValues(records, amount);
return records;
}
Expand All @@ -161,6 +171,7 @@ private class TestModule {
private Map<String, List<SObject>> children = new Map<String, List<SObject>>();
private Template templates;
private RandomizersContainer randomizers = new RandomizersContainer();
private Boolean withFakeId = false;

private SObjectType prototypeType;

Expand All @@ -184,12 +195,12 @@ private class TestModule {
}

public virtual RecordMocker setChildren(String relationship, List<SObject> records) {
this.children.put(relationship, records);
this.children.put(relationship, records.deepClone(true, true, true));
return this;
}

public virtual RecordMocker setFakeId() {
this.fields.put('Id', TestModule.IdGenerator.get(this.prototypeType));
this.withFakeId = true;
return this;
}

Expand All @@ -213,7 +224,9 @@ private class TestModule {
}

public List<SObject> build(Integer amount) {
List<SObject> records = new List<SObject>();
validateRecordsAmount(amount);

List<SObject> records = (List<SObject>) Type.forName('List<' + this.prototypeType + '>').newInstance();
for (Integer i = 0; i < amount; i++) {
records.add(this.buildWithIndex(i));
}
Expand All @@ -224,6 +237,11 @@ private class TestModule {
Map<String, Object> record = new Map<String, Object>();

this.setNested(record);

if (this.withFakeId) {
record.put('Id', TestModule.IdGenerator.get(this.prototypeType));
}

this.setChildren(record);

this.randomizers.randomizeFieldsValues(record, index);
Expand Down Expand Up @@ -272,6 +290,10 @@ private class TestModule {
private List<Object> values;

public ListRandomizer(List<Object> values) {
if (values == null || values.isEmpty()) {
throw new TestModuleException('ListRandomizer requires at least one value.');
}

this.values = values;
}

Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/TestModule.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<apiVersion>67.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 2 additions & 0 deletions force-app/main/default/classes/TestModule_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private class TestModule_Test {
List<SObject> accounts = new AccountTestModule().Builder().build(5);

Assert.areEqual(5, accounts.size());
Assert.areEqual(Account.SObjectType, accounts.getSObjectType(), 'The built list must be concretely typed.');
for (SObject acc : accounts) {
Assert.areEqual('Test Account', acc.get('Name'));
}
Expand Down Expand Up @@ -274,6 +275,7 @@ private class TestModule_Test {
List<SObject> accounts = new AccountTestModule().Mocker().build(3);

Assert.areEqual(3, accounts.size());
Assert.areEqual(Account.SObjectType, accounts.getSObjectType(), 'The built list must be concretely typed.');
for (SObject acc : accounts) {
Assert.areEqual('Test Account', acc.get('Name'));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<apiVersion>67.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion package/main/default/classes/TestModule.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2026 Beyond The Cloud Sp. z o.o. (BeyondTheCloud.Dev)
* Licensed under the MIT License (https://github.com/beyond-the-cloud-dev/test-lib/blob/main/LICENSE)
*
* v0.1.0
* v0.2.0
**/
@IsTest
global class TestModule {
Expand Down
2 changes: 1 addition & 1 deletion package/main/default/classes/TestModule.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<apiVersion>67.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion package/main/default/classes/TestModule_Test.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<apiVersion>67.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"name": "BeyondTheCloud",
"namespace": "btcdev",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "65.0",
"sourceApiVersion": "67.0",
"packageAliases": {
"Test Lib": "0HoP600000002OPKAY",
"Test Lib@0.1.0-1": "04tP600000390yLIAQ"
Expand Down
Loading