public class nIT_TestUtility {
@TestVisible
private Map<String, Integer> typeCounter = new Map<String, Integer>();
public String getFakeId(Schema.SObjectType sot)
{
String typeName = sot.getDescribe().getName();
if (!typeCounter.containsKey(typeName))
typeCounter.put(typeName, 1);
Integer s_num = typeCounter.get(typeName);
String result = String.valueOf(s_num);
String fakeId = sot.getDescribe().getKeyPrefix() + '0'.repeat(12-result.length()) + result;
typeCounter.put(typeName, s_num+1);
return fakeId;
}
//...
}
public class Repository {
//
// Repository definitions
//
public interface IAccountRepo {
List<Account> getAccountData(List<ID> accIdList);
}
//
// Repository implementations
//
public class AccountRepo implements IAccountRepo {
public List<Account> getAccountData(List<ID> accIdList) {
List<Account> accList = [
SELECT Id, Name, Phone
FROM Account
WHERE Id IN :accIdList
];
return accList;
}
}
}
public interface ISObjectRepo {
void insertSo(SObject so);
void updateSo(SObject so);
void deleteSo(SObject so);
void insertSo(List<SObject> soList);
void updateSo(List<SObject> soList);
void deleteSo(List<SObject> soList);
void deleteSoById(ID soId);
void deleteSoById(Set<ID> soIdSet);
}
public class SObjectRepo implements ISObjectRepo {
public void insertSo(SObject so) { INSERT so; }
public void updateSo(SObject so) { UPDATE so; }
public void deleteSo(SObject so) { DELETE so; }
public void insertSo(List<SObject> soList) { INSERT soList; }
public void updateSo(List<SObject> soList) { UPDATE soList; }
public void deleteSo(List<SObject> soList) { DELETE soList; }
public void deleteSoById(ID soId) {
DELETE soId.getSobjectType().newSObject(soId);
}
public void deleteSoById(Set<ID> soIdSet) {
List<SObject> delList = new List<SObject>();
for (ID soId : soIdSet) {
delList.add(soId.getSobjectType().newSObject(soId));
}
DELETE delList;
}
}
public with sharing class rp_Static {
public class DataContext {
public Repository.ISObjectRepo SObjectRepo {
get {
if (privateSObjectRepo == null)
privateSObjectRepo = getISObjectRepo();
return privateSObjectRepo;
}
}
public Repository.IAccountRepo AccountRepo {
get {
if (privateAccountRepo == null)
privateAccountRepo = getIAccountRepo();
return privateAccountRepo;
}
}
@TestVisible
private Repository.ISObjectRepo privateSObjectRepo;
@TestVisible
private Repository.IAccountRepo privateAccountRepo;
}
//
// RepositoryFactories
//
@TestVisible
private static Repository.ISObjectRepo getISObjectRepo() {
Repository.ISObjectRepo repo = new Repository.SObjectRepo();
if (MockSObjectRepo != null && Test.isRunningTest()) {
repo = MockSObjectRepo;
MockSObjectRepo = null;
}
return repo;
}
@TestVisible
private static Repository.IAccountRepo getIAccountRepo() {
Repository.IAccountRepo repo = new Repository.AccountRepo();
if (MockAccountRepo != null && Test.isRunningTest()) {
repo = MockAccountRepo;
MockAccountRepo = null;
}
return repo;
}
//
// Mock repositories
//
@TestVisible
private static Repository.ISObjectRepo MockSObjectRepo {
get;
set;
}
@TestVisible
private static Repository.IAccountRepo MockAccountRepo {
get;
set;
}
}
public class nIT_TestUtility {
@TestVisible
private Map<String, Integer> typeCounter =
new Map<String, Integer>();
public String getFakeId(Schema.SObjectType sot) {
String typeName = sot.getDescribe().getName();
if (!typeCounter.containsKey(typeName))
typeCounter.put(typeName, 1);
Integer s_num = typeCounter.get(typeName);
String fakeId =
sot.getDescribe().getKeyPrefix() +
String.valueOf(s_num).leftPad(12, '0');
typeCounter.put(typeName, s_num + 1);
return fakeId;
}
// ...
public void tempMethod() {
nIT_TestUtility tu = new nIT_TestUtility();
ID testId = tu.getFakeId(Account.SObjectType);
Account acc = (Account)nIT_TestUtility.setNonWritableField(
new Account(),
'IsDeleted',
true
);
}
public static SObject setNonWritableField(
SObject sObj, String fieldName, Object value) {
String jsonString = JSON.serialize(sObj);
Map<String,Object> dataMap =
(Map<String,Object>)JSON.deserializeUntyped(jsonString);
dataMap.put(fieldName, value);
jsonString = JSON.serialize(dataMap);
System.debug(jsonString);
return (SObject)JSON.deserialize(jsonString, SObject.class);
}
public static SObject setRelatedListField(
SObject sObj, String fieldName, List<SObject> items) {
return setNonWritableField(
sObj,
fieldName,
new Map<String, Object>{
'totalSize' => items.size(),
'done' => true,
'records' => items
}
);
}
}