-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAccountDomainTest.cls
465 lines (383 loc) · 17.9 KB
/
AccountDomainTest.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
@IsTest
public with sharing class AccountDomainTest {
private static final UniversalMocker mockService;
private static final AccountDBService mockServiceStub;
private static final AccountDomain sut; // system under test
static {
mockService = UniversalMocker.mock(AccountDBService.class); //This is the service we are mocking
mockServiceStub = (AccountDBService) mockService.createStub();
sut = new AccountDomain(mockServiceStub); //This is the class into which we inject our mocked service
}
@IsTest
public static void it_should_return_one_account() {
//setup
String mockedMethodName = 'getOneAccount';
Account mockAccount = new Account(Name = 'Mock Account');
mockService.when(mockedMethodName).thenReturn(mockAccount);
//test
Test.startTest();
Account accountDetail = sut.getAccountDetail();
Test.stopTest();
//verify
system.assertEquals(mockAccount.Name, accountDetail.Name);
mockService.assertThat().method(mockedMethodName).wasCalled(1);
}
@IsTest
public static void it_should_create_a_public_account() {
//setup
String mockedMethodName = 'doInsert';
//test
Test.startTest();
sut.createPublicAccount('Mock Account');
Test.stopTest();
//verify
Account newAccount = (Account) mockService.forMethod(mockedMethodName).andInvocationNumber(0).getValueOf('acct');
system.assertEquals('Mock Account', newAccount.Name);
system.assertEquals('Public', newAccount.Ownership);
}
@IsTest
public static void it_should_verify_call_counts_correctly() {
//setup
String mockedMethodName = 'getOneAccount';
Account mockAccount = new Account(Name = 'Mock Account');
mockService.when(mockedMethodName).thenReturn(mockAccount);
mockService.when('mockedDummyMethod').thenReturn(null);
//test
Test.startTest();
Account accountDetail = sut.getAccountDetail();
sut.getAccountDetail();
Test.stopTest();
//verify
system.assertEquals(mockAccount.Name, accountDetail.Name);
mockService.assertThat().method(mockedMethodName).wasCalled(1, UniversalMocker.Times.OR_MORE);
mockService.assertThat().method(mockedMethodName).wasCalled(2, UniversalMocker.Times.OR_MORE);
mockService.assertThat().method(mockedMethodName).wasCalled(2);
mockService.assertThat().method(mockedMethodName).wasCalled(2, UniversalMocker.Times.OR_LESS);
mockService.assertThat().method(mockedMethodName).wasCalled(3, UniversalMocker.Times.OR_LESS);
mockService.assertThat().method('mockedDummyMethod').wasNeverCalled();
mockService.assertThat().method('nonMockedDummyMethod').wasNeverCalled();
}
@IsTest
public static void it_should_call_overloaded_methods_correctly() {
//setup
String mockedMethodName = 'getMatchingAccounts';
Account acctOne = new Account(Name = 'Account with matching Id');
Account acctTwo = new Account(Name = 'Account with matching name');
mockService.when(mockedMethodName).withParamTypes(new List<Type>{ Id.class }).thenReturn(new List<Account>{ acctOne });
mockService.when(mockedMethodName).withParamTypes(new List<Type>{ String.class }).thenReturn(new List<Account>{ acctTwo });
//test
Test.startTest();
Id mockAccountId = '001000000000001';
List<Account> acctsWithMatchingId = sut.getMatchingAccounts(mockAccountId);
List<Account> acctsWithMatchingName = sut.getMatchingAccounts('Account with matching name');
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).withParamTypes(new List<Type>{ Id.class }).wasCalled(1);
mockService.assertThat().method(mockedMethodName).withParamTypes(new List<Type>{ String.class }).wasCalled(1);
Id accountIdParam = (Id) mockService.forMethod(mockedMethodName).withParamTypes(new List<Type>{ Id.class }).andInvocationNumber(0).getValueOf('accountId');
String acctNameParam = (String) mockService.forMethod(mockedMethodName)
.withParamTypes(new List<Type>{ String.class })
.andInvocationNumber(0)
.getValueOf('accountName');
System.assertEquals(mockAccountId, accountIdParam);
System.assertEquals('Account with matching name', acctNameParam);
System.assertEquals(acctOne.Name, acctsWithMatchingId[0].Name);
System.assertEquals(acctTwo.Name, acctsWithMatchingName[0].Name);
}
@IsTest
public static void shouldResetMethodAndParamsAfterEachChain() {
//setup
String mockedMethodName = 'getMatchingAccounts';
String newMethodName = 'getOneAccount';
Account acctOne = new Account(Name = 'Account with matching Id');
Account acctTwo = new Account(Name = 'Account with matching name');
mockService.when(mockedMethodName).withParamTypes(new List<Type>{ Id.class }).thenReturn(new List<Account>{ acctOne });
mockService.when(newMethodName).thenReturn(acctTwo); //This method takes no params. So by mocking this we attempt to ensure that the param type list from previous mock call (Id.class) has been cleared out
//test
Test.startTest();
Id mockAccountId = '001000000000001';
List<Account> acctsWithMatchingId = sut.getMatchingAccounts(mockAccountId);
Account anotherAccount = sut.getAccountDetail();
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).withParamTypes(new List<Type>{ Id.class }).wasCalled(1);
mockService.assertThat().method(newMethodName).wasCalled(1);
System.assertEquals(acctOne.Name, acctsWithMatchingId[0].Name);
System.assertEquals(acctTwo.Name, anotherAccount.Name);
Id acctIdParam = (Id) mockService.forMethod(mockedMethodName).withParamTypes(new List<Type>{ Id.class }).getValueOf('accountId');
Map<String, Object> argsMap = mockService.forMethod(newMethodName).getArgumentsMap();
Assert.areEqual(mockAccountId, acctIdParam);
Assert.areEqual(0, argsMap.size());
}
@IsTest
public static void it_should_throw_mock_exception() {
//setup
String mockedMethodName = 'doInsert';
String mockExceptionMessage = 'Mock exception';
AuraHandledException mockException = new AuraHandledException(mockExceptionMessage);
/*https://salesforce.stackexchange.com/questions/122657/testing-aurahandledexceptions*/
mockException.setMessage(mockExceptionMessage);
mockService.when(mockedMethodName).thenThrow(mockException);
//test
Test.startTest();
boolean hasException = false;
try {
sut.createPublicAccount('Mock Account');
} catch (AuraHandledException ex) {
System.assertEquals(mockExceptionMessage, ex.getMessage());
hasException = true;
}
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).wasCalled(1);
System.assert(hasException, 'Mocked exception was not thrown');
}
@IsTest
public static void it_should_generate_unique_guids() {
Integer numInstances = 20000;
for (Integer i = 0; i < numInstances; i++) {
UniversalMocker uMock = UniversalMocker.mock(AccountDBService.class);
}
System.assertEquals(numInstances + 1, UniversalMocker.uMockInstances.size(), 'We have collision in the generated guids'); //numInstances + 1 generated in the static block above
}
@IsTest
public static void it_should_track_call_counts_across_queueables() {
String mockedMethodName = 'doInsert';
String mockExceptionMessage = 'Mock exception';
UniversalMocker.Mutator dmlMutatorInstance = new DMLMutator();
mockService.when(mockedMethodName).mutateWith(dmlMutatorInstance).thenReturnVoid();
AccountsQueuable queueableSut = new AccountsQueuable(sut);
//test
Test.startTest();
System.enqueueJob(queueableSut);
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).wasCalled(1);
Account acct = (Account) mockService.forMethod(mockedMethodName).getValueOf('acct');
System.assertNotEquals(null, acct.Id, 'Account Id is null after insert');
}
@IsTest
public static void shouldApplyMutatorsBasedOnCallCounts() {
String mockedMethodName = 'doUpdate';
String mockExceptionMessage = 'Mock exception';
UniversalMocker.Mutator dmlMutatorInstance = new DMLMutator();
mockService.when(mockedMethodName).mutateUntil(1, new DescriptionMutator('1')).thenReturnVoid();
Account acct = new Account(Name = 'Acme Inc');
sut.updateAccount(acct);
sut.updateAccount(acct);
sut.updateAccount(acct);
//verify
mockService.assertThat().method(mockedMethodName).wasCalled(3);
Assert.areEqual('1', acct.Description, 'Expected description to only be set once (till callcount 1)');
mockService.resetState();
acct.Description = ''; //reset account description
mockService.when(mockedMethodName)
.mutateUntil(2, new DescriptionMutator('1'))
.mutateUntil(2, new DescriptionMutator('2'))
.mutateWith(new DescriptionMutator('3'))
.thenReturnVoid();
sut.updateAccount(acct);
sut.updateAccount(acct);
sut.updateAccount(acct);
Assert.areEqual(
'12123',
acct.Description,
'Expected description to set to "12123" ("12" appended for each of the first two calls and "3" appended for the third call'
);
}
@IsTest
public static void it_should_track_call_counts_with_batchables() {
String mockedMethodName = 'getOneAccount';
Account mockAccount = new Account(Name = 'Mock Account');
mockService.when(mockedMethodName).thenReturn(mockAccount);
AccountsBatch batchableSut = new AccountsBatch(sut);
//test
Test.startTest();
Database.executeBatch(batchableSut, 1);
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).wasCalled(1);
}
@IsTest
public static void it_should_mutate_arguments() {
//setup
String mockedMethodName = 'doInsert';
String mockExceptionMessage = 'Mock exception';
UniversalMocker.Mutator dmlMutatorInstance = new DMLMutator();
mockService.when(mockedMethodName).mutateWith(dmlMutatorInstance).thenReturnVoid();
//test
Test.startTest();
boolean hasException = false;
try {
sut.createPublicAccount('Mock Account');
} catch (AuraHandledException ex) {
System.assertEquals(mockExceptionMessage, ex.getMessage());
hasException = true;
}
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).wasCalled(1);
System.assert(!hasException, 'Mocked exception was not thrown');
Account acct = (Account) mockService.forMethod('doInsert').getValueOf('acct');
System.assertNotEquals(null, acct.Id, 'Account Id is null after insert');
}
@IsTest
public static void it_should_handle_multiple_return_values_basic() {
//setup
String mockedMethodName = 'getOneAccount';
Account mockAccountOne = new Account(Name = 'Mock Account One');
Account mockAccountTwo = new Account(Name = 'Mock Account Two');
mockService.when(mockedMethodName).thenReturnUntil(1, mockAccountOne).thenReturn(mockAccountTwo);
//test
Test.startTest();
Account accountDetail = sut.getAccountDetail();
Assert.areEqual(mockAccountOne.Name, accountDetail.Name);
accountDetail = sut.getAccountDetail();
Assert.areEqual(mockAccountTwo.Name, accountDetail.Name);
//should return mockAccountTwo for all subsequent calls
for (Integer i = 0; i < 100; i++) {
accountDetail = sut.getAccountDetail();
}
Assert.areEqual(mockAccountTwo.Name, accountDetail.Name);
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).wasCalled(102);
}
@IsTest
public static void it_should_handle_multiple_return_values_advanced() {
//setup
String mockedMethodName = 'getOneAccount';
Account mockAccountOne = new Account(Name = 'Mock Account One');
Account mockAccountTwo = new Account(Name = 'Mock Account Two');
Account mockAccountThree = new Account(Name = 'Mock Account Three');
//returns mockAccountOne for the first call, mockAccountTwo for the next 2 calls, and mockAccountThree for all subsequent calls
mockService.when(mockedMethodName).thenReturnUntil(1, mockAccountOne).thenReturnUntil(3, mockAccountTwo).thenReturn(mockAccountThree);
//test
Test.startTest();
Account accountDetail = sut.getAccountDetail();
Assert.areEqual(mockAccountOne.Name, accountDetail.Name);
accountDetail = sut.getAccountDetail();
Assert.areEqual(mockAccountTwo.Name, accountDetail.Name);
accountDetail = sut.getAccountDetail();
Assert.areEqual(mockAccountTwo.Name, accountDetail.Name);
accountDetail = sut.getAccountDetail();
Assert.areEqual(mockAccountThree.Name, accountDetail.Name);
//should return mockAccountTwo for all subsequent calls
for (Integer i = 0; i < 100; i++) {
accountDetail = sut.getAccountDetail();
}
Assert.areEqual(mockAccountThree.Name, accountDetail.Name);
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).wasCalled(104);
}
@IsTest
public static void it_should_handle_multiple_return_values_exception() {
//setup
String mockedMethodName = 'getOneAccount';
Account mockAccountOne = new Account(Name = 'Mock Account One');
String mockExceptionMessage = 'Mock exception';
AuraHandledException mockException = new AuraHandledException(mockExceptionMessage);
mockException.setMessage(mockExceptionMessage);
mockService.when(mockedMethodName).thenThrowUntil(2, mockException).thenReturn(mockAccountOne);
//test
Test.startTest();
try {
Account accountDetail = sut.getAccountDetail();
Assert.fail('Expected exception to be thrown');
} catch (AuraHandledException ex) {
Assert.areEqual(mockExceptionMessage, ex.getMessage());
}
try {
Account accountDetail = sut.getAccountDetail();
Assert.fail('Expected exception to be thrown');
} catch (AuraHandledException ex) {
Assert.areEqual(mockExceptionMessage, ex.getMessage());
}
Account accountDetail = sut.getAccountDetail();
Assert.areEqual(mockAccountOne.Name, accountDetail.Name);
//should return mockAccountTwo for all subsequent calls
for (Integer i = 0; i < 100; i++) {
accountDetail = sut.getAccountDetail();
}
Assert.areEqual(mockAccountOne.Name, accountDetail.Name);
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).wasCalled(103);
}
@IsTest
public static void it_should_call_overloaded_methods_multiple_return_values() {
//setup
String mockedMethodName = 'getMatchingAccounts';
Account acctByIdOne = new Account(Name = 'Account with matching Id One');
Account acctByIdTwo = new Account(Name = 'Account with matching Id Two');
Account acctByNameOne = new Account(Name = 'Account with matching name');
mockService.when(mockedMethodName)
.withParamTypes(new List<Type>{ Id.class })
.thenReturnUntil(2, new List<Account>{ acctByIdOne })
.thenReturn(new List<Account>{ acctByIdTwo });
mockService.when(mockedMethodName).withParamTypes(new List<Type>{ String.class }).thenReturn(new List<Account>{ acctByNameOne });
//test
Test.startTest();
Id mockAccountId = '001000000000001';
List<Account> acctsWithMatchingId = sut.getMatchingAccounts(mockAccountId);
Assert.areEqual(acctByIdOne.Name, acctsWithMatchingId[0].Name);
acctsWithMatchingId = sut.getMatchingAccounts(mockAccountId);
Assert.areEqual(acctByIdOne.Name, acctsWithMatchingId[0].Name);
acctsWithMatchingId = sut.getMatchingAccounts(mockAccountId);
Assert.areEqual(acctByIdTwo.Name, acctsWithMatchingId[0].Name);
for (Integer i = 0; i < 100; i++) {
acctsWithMatchingId = sut.getMatchingAccounts(mockAccountId);
}
List<Account> acctsWithMatchingName = sut.getMatchingAccounts('Account with matching name');
Test.stopTest();
//verify
mockService.assertThat().method(mockedMethodName).withParamTypes(new List<Type>{ Id.class }).wasCalled(103);
mockService.assertThat().method(mockedMethodName).withParamTypes(new List<Type>{ String.class }).wasCalled(1);
Id accountIdParam = (Id) mockService.forMethod(mockedMethodName).withParamTypes(new List<Type>{ Id.class }).andInvocationNumber(50).getValueOf('accountId');
String acctNameParam = (String) mockService.forMethod(mockedMethodName)
.withParamTypes(new List<Type>{ String.class })
.andInvocationNumber(0)
.getValueOf('accountName');
Assert.areEqual(mockAccountId, accountIdParam);
Assert.areEqual('Account with matching name', acctNameParam);
Assert.areEqual(acctByIdTwo.Name, acctsWithMatchingId[0].Name);
Assert.areEqual(acctByNameOne.Name, acctsWithMatchingName[0].Name);
}
@IsTest
public static void dummy_test_for_db_service() {
AccountDBService dbSvc = new AccountDBService();
Account a = new Account(Name = 'Acme');
dbSvc.doInsert(a);
dbSvc.getOneAccount();
dbSvc.getMatchingAccounts(Id.valueOf('001000000000001'));
dbSvc.getMatchingAccounts('Acme');
}
//Adds a given suffix to account description
public class DescriptionMutator implements UniversalMocker.Mutator {
private String stringToAdd = '';
public DescriptionMutator(String stringToAdd) {
this.stringToAdd = stringToAdd;
}
public void mutate(Object stubbedObject, String stubbedMethodName, List<Type> listOfParamTypes, List<Object> listOfArgs) {
Account record = (Account) listOfArgs[0];
if (record.get('Description') != null) {
record.Description += this.stringToAdd;
} else {
record.Description = this.stringToAdd;
}
}
}
public class DMLMutator implements UniversalMocker.Mutator {
// Ideally, 'fakeCounter' should be a static variable and 'getFakeId' should be a static method in another top-level class.
private Integer fakeIdCounter = 1;
public String getFakeId(Schema.SObjectType objType) {
String result = String.valueOf(this.fakeIdCounter++);
return objType.getDescribe().getKeyPrefix() + '0'.repeat(12 - result.length()) + result;
}
public void mutate(Object stubbedObject, String stubbedMethodName, List<Type> listOfParamTypes, List<Object> listOfArgs) {
Account record = (Account) listOfArgs[0];
record.Id = this.getFakeId(Account.SObjectType);
}
}
}