-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMockClassTest.cls
141 lines (128 loc) · 5.27 KB
/
MockClassTest.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
@IsTest
private with sharing class MockClassTest {
@IsTest
private static void handleMethodCall_handlesMethodCall() {
// given
MockClass mockClass = new MockClass();
MockMethod mockMethod = new MockMethod(
'mockMethod',
new List<Object>{ 'returnValue' }
);
mockClass.addMockMethod(mockMethod);
// when
String result = (String) mockClass.handleMethodCall(
null,
'mockMethod',
null,
null,
null,
null
);
// then
System.assertEquals('returnValue', result);
}
@IsTest
private static void addMockMethod_throwsExceptionIfMethodMockedMoreThanOnce() {
// given
MockClass mockClass = new MockClass();
MockMethod mockMethod = new MockMethod(
'mockMethod',
new List<Object>{ 'returnValue' }
);
mockClass.addMockMethod(mockMethod);
// when
Exception thrownException;
try {
mockClass.addMockMethod(mockMethod);
} catch (Exception e) {
thrownException = e;
}
// then
System.assertNotEquals(null, thrownException);
System.assertEquals(
'mockMethod is already mocked',
thrownException.getMessage()
);
}
@IsTest
private static void handleMethodCall_throwsExceptionIfMethodIsNotMocked() {
// given
MockClass mockClass = new MockClass();
// when
Exception thrownException;
try {
mockClass.handleMethodCall(null, 'mockMethod', null, null, null, null);
} catch (Exception e) {
thrownException = e;
}
// then
System.assertNotEquals(null, thrownException);
System.assertEquals(
'mockMethod is not mocked',
thrownException.getMessage()
);
}
@isTest
private static void handleMethodCall_storesInputsForAllCalls() {
// given
MockClass mockClass = new MockClass();
MockMethod mockMethod = new MockMethod(
'mockMethod',
new List<Object>{ 'returnValue', 'returnValue3' }
);
mockClass.addMockMethod(mockMethod);
MockMethod mockMethod2 = new MockMethod(
'mockMethod2',
new List<Object>{ 'returnValue2' }
);
mockClass.addMockMethod(mockMethod2);
// when
Account accountObject = new Account();
mockClass.handleMethodCall(
accountObject,
'mockMethod',
List<Account>.class,
new List<Type> { String.class, Integer.class },
new List<String> { 'arg1', 'arg2' },
new List<Object> { 'arg1Value', 2 }
);
mockClass.handleMethodCall(
accountObject,
'mockMethod2',
List<Contact>.class,
new List<Type> { Boolean.class, Double.class },
new List<String> { 'arg3', 'arg4' },
new List<Object> { 'arg3Value', 2 }
);
mockClass.handleMethodCall(
accountObject,
'mockMethod',
List<Account>.class,
new List<Type> { String.class, Integer.class },
new List<String> { 'arg1', 'arg2' },
new List<Object> { 'arg1Value2', 7 }
);
// then
MockMethodCall firstCall = mockClass.calls[0];
Assert.areEqual(accountObject, firstCall.stubbedObject, 'first call object should be the accountObject');
Assert.areEqual('mockMethod', firstCall.stubbedMethodName, 'first call should be to mockMethod');
Assert.areEqual(List<Account>.class, firstCall.returnType, 'first call return type should be List<Account>.class');
Assert.areEqual(new List<Type> { String.class, Integer.class }, firstCall.listOfParamTypes, 'first call param types should be [String.class, Integer.class]');
Assert.areEqual(new List<String> { 'arg1', 'arg2' }, firstCall.listOfParamNames, 'first call param names should be [arg1, arg2]');
Assert.areEqual(new List<Object> { 'arg1Value', 2 }, firstCall.listOfArgs, 'first call args should be [arg1Value, 2]');
MockMethodCall secondCall = mockClass.calls[1];
Assert.areEqual(accountObject, secondCall.stubbedObject, 'second call object should be the accountObject');
Assert.areEqual('mockMethod2', secondCall.stubbedMethodName, 'second call should be to mockMethod2');
Assert.areEqual(List<Contact>.class, secondCall.returnType, 'second call return type should be List<Contact>.class');
Assert.areEqual(new List<Type> { Boolean.class, Double.class }, secondCall.listOfParamTypes, 'second call param types should be [Boolean.class, Double.class]');
Assert.areEqual(new List<String> { 'arg3', 'arg4' }, secondCall.listOfParamNames, 'second call param names should be [arg3, arg4]');
Assert.areEqual(new List<Object> { 'arg3Value', 2 }, secondCall.listOfArgs, 'second call args should be [arg3Value, 2]');
MockMethodCall thirdCall = mockClass.calls[2];
Assert.areEqual(accountObject, thirdCall.stubbedObject, 'third call object should be the accountObject');
Assert.areEqual('mockMethod', thirdCall.stubbedMethodName, 'third call should be to mockMethod');
Assert.areEqual(List<Account>.class, thirdCall.returnType, 'third call return type should be List<Account>.class');
Assert.areEqual(new List<Type> { String.class, Integer.class }, thirdCall.listOfParamTypes, 'third call param types should be [String.class, Integer.class]');
Assert.areEqual(new List<String> { 'arg1', 'arg2' }, thirdCall.listOfParamNames, 'third call param names should be [arg1, arg2]');
Assert.areEqual(new List<Object> { 'arg1Value2', 7 }, thirdCall.listOfArgs, 'third call args should be [arg1Value2, 7]');
}
}