apexfarm / apextestkit Goto Github PK
View Code? Open in Web Editor NEWA way to simplify your Salesforce data creation and method stubbing.
License: Apache License 2.0
A way to simplify your Salesforce data creation and method stubbing.
License: Apache License 2.0
Will bring java mockito library BDD API into ATK. Something like the following will be implemented. The feature is still in its early design/development phase, please leave comments if some features want to be considered. You can also suggest how to define the APIs if they are more concise. Target to deliver in August this year 2021.
SomeClassOrInterface mock = (SomeClassOrInterface)ATK.mock(SomeClassOrInterface.class);
{// GIVEN
ATK.given(mock.doWithInteger(0)).willReturn(0);
ATK.given(mock.doWithInteger(ATK.anyInteger())).willReturn(1);
ATK.given(mock.doWithInteger(2)).willAnswer(new IntegerAnswer());
ATK.given(mock.doWithInteger(3)).willThrow(exp);
((ATKMockTest)ATK.willReturn(4).given(mock)).doWithInteger(4);
((ATKMockTest)ATK.willThrow(exp).given(mock)).doException(); // void method
((ATKMockTest)ATK.willDoNothing().given(mock)).doNothing(); // void method
}
{// WHEN
mock.doWithInteger(0);
mock.doWithInteger(1);
mock.doWithInteger(2);
try {
mock.doWithInteger(3);
} catch (MockException ex) {
}
mock.doWithInteger(4);
try {
mock.doException();
} catch (MockException ex) {
}
mock.doNothing();
}
{// THEN
((ATKMockTest)ATK.then(mock).should().times(1)).doWithInteger(0);
((ATKMockTest)ATK.then(mock).should().atLeast(1)).doWithInteger(ATK.anyInteger());
((ATKMockTest)ATK.then(mock).should().atMost(1)).doWithInteger(2);
((ATKMockTest)ATK.then(mock).should().never()).doWithInteger(5);
ATK.InOrder inOrder = ATK.inOrder(mock);
((ATKMockTest)ATK.then(mock).should(inOrder).times(1)).doWithInteger(0);
((ATKMockTest)ATK.then(mock).should(inOrder).atLeast(1)).doWithInteger(ATK.anyInteger());
((ATKMockTest)ATK.then(mock).should(inOrder).times(1)).doWithInteger(2);
((ATKMockTest)ATK.then(mock).should(inOrder).times(1)).doWithInteger(3);
}
I feel it is very encouraging when stars go up, and also a big thank-you for you to choose this library.
We're installed this package on our org. The test case test_Util_Converter is failing on this line.
It could be a timezone thing, since the default timezone is Chicago time.
Error message: | System.AssertException: Assertion Failed: Expected: 2021-01-16, Actual: 2021-01-15
-- | --
Stack trace: | Class.ATKCoreTest.test_Util_Converter: line 972, column 1
Most of the time, there is only going to be one lookup field to a specific sObject type. In such case, writing referenceBy()
seems tedious for developers.
As this library main use setup data for test classes so why didn't use IsTest annotation in main classes. As IsTest class will not count in Apex Char Limit.
To reuse common field values we often use EntityBuilder, for example
`@IsTest
private class AtkBugTest {
@IsTest
private static void test1() {
ATK.SaveResult atkResult = ATK.prepare(Opportunity.SObjectType, 10)
.build(new OpportunityEntityBuilder())
.field(Opportunity.Name).repeat('Test Name')
.mock();
List<Opportunity> opportunities = (List<Opportunity>) atkResult.get(Opportunity.SObjectType);
for (Opportunity opp : opportunities) {
Assert.areEqual('Test Name', opp.Name); // this fails, actual result is OPPORTUNITY-NAME-1
}
}
public class OpportunityEntityBuilder implements ATK.EntityBuilder {
public void build(ATK.Entity entity, Integer size) {
entity
.field(Opportunity.Name).index('OPPORTUNITY-NAME-{0}')
.field(Opportunity.Description).repeat('Test Description')
.field(Opportunity.CloseDate).repeat(Date.today().addMonths(1));
}
}
}`
The library should be fine tested, in order to persuade more usage in the community. I should write as many robust test methods as I can.
The sObjects built within one statement of the ATK command, has to be strict directed acyclic graph. The library will enforce the checking of the DAG in future release.
As discussed with colleague today. I realized, the library doesn't support dependent field selection. With a bit of search online, I am going to follow the strategy mentioned in:
Just to think one step further, how about select picklist values based on record types? However, after googling it, this can only be achieved from User Interface API, or Metadata API, which are not an option during unit testing.
Using 3.3.1
version the next code gives the result:
Id userId = '005000000000003AAA';
Id managerId = '005000000000004AAA';
List<User> users = (List<User>) ATK.prepare(User.SObjectType, 1)
.profile('System Administrator')
.field(User.Id).repeat(userId)
.field(User.LastName).repeat('userLastName')
.field(User.Name).repeat('userName')
.field(User.ManagerId).repeat(managerId)
.mock()
.get(User.SObjectType);
System.debug(LoggingLevel.INFO, 'users ' + JSON.serializePretty(users));
/*
{
"attributes" : {
"type" : "User",
"url" : "/services/data/v51.0/sobjects/User/005000000000003AAA"
},
"ProfileId" : "00e1j000000htRQAAY",
"Id" : "005000000000003AAA",
"LastName" : "userLastName",
"ManagerId" : "005000000000004AAA",
"Name" : "userName"
}
*/
Using 3.4.1
version the next code gives the result:
Id userId = '005000000000003AAA';
Id managerId = '005000000000004AAA';
List<User> users = (List<User>) ATK.prepare(User.SObjectType, 1)
.profile('System Administrator')
.field(User.Id).repeat(userId)
.field(User.LastName).repeat('userLastName')
.field(User.Name).repeat('userName')
.field(User.ManagerId).repeat(managerId)
.mock()
.get(User.SObjectType);
System.debug(LoggingLevel.INFO, 'users ' + JSON.serializePretty(users));
/*
{
"attributes" : {
"type" : "User",
"url" : "/services/data/v51.0/sobjects/User/005000zzzz00001AAA"
},
"ProfileId" : "00e1j000000htRQAAY",
"Id" : "005000zzzz00001AAA",
"LastName" : "userLastName",
"ManagerId" : "005000000000004AAA",
"Name" : "userName"
}
*/
Somehow in 3.4.1
the User.Id
field is being overridden by the newly generated Id
value regardless we use .field(User.Id).repeat(userId)
method invocation or not. Although ManagerId
lookup wasn't overridden.
Is it expected behavior and what should we do in case if we want to set the Id
from the outside?
I've raised the issue at least because the 3.4.1
version breaks the backward compatibility with 3.3.1
for such a specific scenario.
With this framework, what is the recommended way to autonumber for the save method?
After creating an object with children, object.childrens is empty.
For example:
List<Account> accountMocks = (List<Account>) ATK.prepare(Account.SObjectType, 1)
.field(Account.Name).repeat('Name')
.withChildren(Contact.SObjectType, Contact.AccountId, 1)
.mock().get(Account.SObjectType);
System.debug(accountMocks[0]); //Account:{Name=Name, Id=001000zzzz00001AAA}
System.debug(accountMocks[0].Contacts); //()
List<Account> accountsSavedFalse = (List<Account>) ATK.prepare(Account.SObjectType, 1)
.field(Account.Name).repeat('Name')
.withChildren(Contact.SObjectType, Contact.AccountId, 1)
.save(false).get(Account.SObjectType);
System.debug(accountsSavedFalse[0]); //Account:{Name=Name}
System.debug(accountsSavedFalse[0].Contacts); //()
List<Account> accountsSaved = (List<Account>) ATK.prepare(Account.SObjectType, 1)
.field(Account.Name).repeat('Name')
.withChildren(Contact.SObjectType, Contact.AccountId, 1)
.save().get(Account.SObjectType);
System.debug(accountsSaved[0]); //Account:{Name=Name, Id=0011X00000jKZvoQAG}
System.debug(accountsSaved[0].Contacts); //()
Currently string is generated by pick up random greek words. However this is not efficient for unique string generation. I will provide hashcode like faker format for text fields.
Stack limitation exception is raised when Apex debug level is finest, here is how to reproduce the issue:
ATK.SaveResult result = ATK.prepare(Account.SObjectType, 1)
.withChildren(Contact.SObjectType, Contact.AccountId, 1)
.mock();
Other Apex debug level will have no such problem.
=====================Known SF Issue======================
https://trailblazer.salesforce.com/issues_view?id=a1p3A000001Gv4KQAS
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, We can't enable this file because its file type isn't allowed for asset files.: [FileType]
ATKWizard.I().wantMany('ContentVersion')
.total(3)
.fields(new Map<String, Object> {
'Title' => 'file_title_{{###}}',
'PathOnClient' => 'file_path_{{###}}',
'VersionData' => EncodingUtil.base64Decode('Unit Test Attachment Body')
})
.generate();
Suspect some of the required fields are not generated correctly.
After discussion with my colleague, they sometime have to create records based on sequential dates. For example:
Initially, I think this could not be possible. But after a second thought, I came up with some immature ideas. I can create some arithmetic expressions along side with a parameter pattern to reference dynamic record such as
fields(new Map<String, Object> {
'StartDate__c' => '{!dates.addDay($1.EndDate__c, 1)}',
'EndDate__c' => '{!dates.addMonth($1.EndDate__c, 1)}',
'Bigger__c' => '{!number.add($0.Smaller__c, 10)}'
})
Could this be useful? or it just complicates the generation logic. Anything simpler can be achieved?
This proposal is to make the main ATK class to contain two save
methods. One with a boolean in its signature (which is the one present today) and another one without parameters, that implies that the developer wants to save the records ("true" is used).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.