Skip to content

Commit 3df84dd

Browse files
committed
feat(StubBuilder): Adding AnyParameter
Stub system now allows for the specification of 'ParameterMatcher.ANYVALUE' as a value for withParameterValues() call, which desensitizes the built stub to the input's submitted. This functionally means that you can contstruct a stub that accepts any parameter.
1 parent 7cedb38 commit 3df84dd

8 files changed

Lines changed: 131 additions & 99 deletions

File tree

.idea/ApexKit.iml

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dictionaries/project.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-926 Bytes
Binary file not shown.

force-app/main/default/classes/test utilities/MockedMethod.cls

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,12 @@ public with sharing class MockedMethod {
154154
* values - at runtime - of the method call.
155155
* @return `Boolean`
156156
*/
157-
public Boolean doMethodSignaturesAndParametersMatch(MethodSignature methodSignature, List<Object> runtimeParameters) {
158-
return this.methodSignature.verifySignatureMatch(methodSignature) && doRuntimeParametersMatch(runtimeParameters);
157+
public Boolean doMethodSignaturesAndParametersMatch(
158+
MethodSignature methodSignature,
159+
List<Object> runtimeParameters
160+
) {
161+
return this.methodSignature.verifySignatureMatch(methodSignature) &&
162+
doRuntimeParametersMatch(runtimeParameters);
159163
}
160164

161165
/**
@@ -175,16 +179,18 @@ public with sharing class MockedMethod {
175179
break;
176180
}
177181

178-
// If the expected parameter is ParameterMatcher.ANY, we accept any value for this parameter
182+
// If the expected parameter is ParameterMatcher.ANYPARAMETER, we accept any value for this parameter
179183
if (
180184
this.expectedParameters[i] instanceof ParameterMatcher &&
181-
this.expectedParameters[i] == ParameterMatcher.ANYPARAMETER
185+
this.expectedParameters[i] == ParameterMatcher.ANYVALUE
182186
) {
183187
// Skip equality check for this parameter - it automatically matches
184188
continue;
185189
}
186190

187-
matchesSoFar &= String.valueOf(this.expectedParameters[i]) == String.valueOf(compareTo[i]);
191+
matchesSoFar &=
192+
String.valueOf(this.expectedParameters[i]) ==
193+
String.valueOf(compareTo[i]);
188194
}
189195
return matchesSoFar;
190196
}
@@ -210,7 +216,10 @@ public with sharing class MockedMethod {
210216
* @param methodSignatureBuilder MethodSignature.Builder object to use
211217
* @param args List of System.Type
212218
*/
213-
public Builder(MethodSignature.Builder methodSignatureBuilder, List<System.Type> args) {
219+
public Builder(
220+
MethodSignature.Builder methodSignatureBuilder,
221+
List<System.Type> args
222+
) {
214223
this.runtimeParameters = args;
215224
this.methodSignatureBuilder = methodSignatureBuilder;
216225
}
@@ -241,7 +250,10 @@ public with sharing class MockedMethod {
241250
* @param parameter2 System.Type
242251
* @return this
243252
*/
244-
public MockedMethod.Builder withParameterValues(Object parameter, Object parameter2) {
253+
public MockedMethod.Builder withParameterValues(
254+
Object parameter,
255+
Object parameter2
256+
) {
245257
return this.setParameterValues(new List<Object>{ parameter, parameter2 });
246258
}
247259

@@ -252,8 +264,14 @@ public with sharing class MockedMethod {
252264
* @param parameter3 System.Type
253265
* @return return description
254266
*/
255-
public MockedMethod.Builder withParameterValues(Object parameter, Object parameter2, Object parameter3) {
256-
return this.setParameterValues(new List<Object>{ parameter, parameter2, parameter3 });
267+
public MockedMethod.Builder withParameterValues(
268+
Object parameter,
269+
Object parameter2,
270+
Object parameter3
271+
) {
272+
return this.setParameterValues(
273+
new List<Object>{ parameter, parameter2, parameter3 }
274+
);
257275
}
258276

259277
/**
@@ -271,7 +289,9 @@ public with sharing class MockedMethod {
271289
Object parameter3,
272290
Object parameter4
273291
) {
274-
return this.setParameterValues(new List<Object>{ parameter, parameter2, parameter3, parameter4 });
292+
return this.setParameterValues(
293+
new List<Object>{ parameter, parameter2, parameter3, parameter4 }
294+
);
275295
}
276296

277297
/**
@@ -341,7 +361,8 @@ public with sharing class MockedMethod {
341361
* @return `MockedMethod`
342362
*/
343363
public MockedMethod createMockedMethod(MethodSignature signature) {
344-
MockedMethod mockedMethod = new MockedMethod(signature).withParameterValues(runtimeParameters);
364+
MockedMethod mockedMethod = new MockedMethod(signature)
365+
.withParameterValues(runtimeParameters);
345366
if (returnSObjectIds != null) {
346367
mockedMethod.returning(returnSObjectIds);
347368
}

force-app/main/default/classes/test utilities/ParameterMatcher.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ public enum ParameterMatcher {
66
/**
77
* @description Matches any parameter value regardless of the actual runtime value
88
*/
9-
ANYPARAMETER
9+
ANYVALUE
1010
}
Lines changed: 90 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,92 @@
11
@IsTest
22
private class ParameterMatcherTest {
3-
4-
private class TestClass {
5-
public String methodWithParameters(String param1, Integer param2) {
6-
return param1 + String.valueOf(param2);
7-
}
8-
}
9-
10-
@IsTest
11-
static void testAnyParameterMatcher() {
12-
// Arrange
13-
TestClass mockObject = (TestClass) new Stub.Builder(TestClass.class)
14-
.mockingMethodCall('methodWithParameters')
15-
.withParameterTypes(String.class, Integer.class)
16-
.withParameterValues('testValue', ParameterMatcher.ANY)
17-
.returning('mocked result')
18-
.defineStub(true);
19-
20-
// Act
21-
String result1 = mockObject.methodWithParameters('testValue', 42);
22-
String result2 = mockObject.methodWithParameters('testValue', 999);
23-
24-
// Assert
25-
Assert.areEqual('mocked result', result1, 'Should return mocked result with any second parameter (42)');
26-
Assert.areEqual('mocked result', result2, 'Should return mocked result with any second parameter (999)');
27-
}
28-
29-
@IsTest
30-
static void testMultipleAnyParameterMatchers() {
31-
// Arrange
32-
TestClass mockObject = (TestClass) new Stub.Builder(TestClass.class)
33-
.mockingMethodCall('methodWithParameters')
34-
.withParameterTypes(String.class, Integer.class)
35-
.withParameterValues(ParameterMatcher.ANY, ParameterMatcher.ANY)
36-
.returning('any params result')
37-
.defineStub(true);
38-
39-
// Act
40-
String result1 = mockObject.methodWithParameters('value1', 42);
41-
String result2 = mockObject.methodWithParameters('value2', 999);
42-
43-
// Assert
44-
Assert.areEqual('any params result', result1, 'Should return mocked result with any parameters (1)');
45-
Assert.areEqual('any params result', result2, 'Should return mocked result with any parameters (2)');
46-
}
47-
48-
@IsTest
49-
static void testMixedExactAndAnyMatchers() {
50-
// Arrange
51-
TestClass mockObject = (TestClass) new Stub.Builder(TestClass.class)
52-
// First stub with exact first parameter and ANY second parameter
53-
.mockingMethodCall('methodWithParameters')
54-
.withParameterTypes(String.class, Integer.class)
55-
.withParameterValues('exact', ParameterMatcher.ANY)
56-
.returning('exact first, any second')
57-
// Second stub with ANY first parameter and exact second parameter
58-
.mockingMethodCall('methodWithParameters')
59-
.withParameterTypes(String.class, Integer.class)
60-
.withParameterValues(ParameterMatcher.ANY, 42)
61-
.returning('any first, exact second')
62-
.defineStub(true);
63-
64-
// Act & Assert
65-
Assert.areEqual(
66-
'exact first, any second',
67-
mockObject.methodWithParameters('exact', 999),
68-
'Should match first stub with exact first parameter'
69-
);
70-
71-
Assert.areEqual(
72-
'any first, exact second',
73-
mockObject.methodWithParameters('different', 42),
74-
'Should match second stub with exact second parameter'
75-
);
76-
}
77-
}
3+
private class TestClass {
4+
public String methodWithParameters(String param1, Integer param2) {
5+
return param1 + String.valueOf(param2);
6+
}
7+
}
8+
9+
@IsTest
10+
static void testAnyParameterMatcher() {
11+
// Arrange
12+
TestClass mockObject = (TestClass) new Stub.Builder(TestClass.class)
13+
.mockingMethodCall('methodWithParameters')
14+
.withParameterTypes(String.class, Integer.class)
15+
.withParameterValues('testValue', ParameterMatcher.ANYVALUE)
16+
.returning('mocked result')
17+
.defineStub(true);
18+
19+
// Act
20+
String result1 = mockObject.methodWithParameters('testValue', 42);
21+
String result2 = mockObject.methodWithParameters('testValue', 999);
22+
23+
// Assert
24+
Assert.areEqual(
25+
'mocked result',
26+
result1,
27+
'Should return mocked result with any second parameter (42)'
28+
);
29+
Assert.areEqual(
30+
'mocked result',
31+
result2,
32+
'Should return mocked result with any second parameter (999)'
33+
);
34+
}
35+
36+
@IsTest
37+
static void testMultipleAnyParameterMatchers() {
38+
// Arrange
39+
TestClass mockObject = (TestClass) new Stub.Builder(TestClass.class)
40+
.mockingMethodCall('methodWithParameters')
41+
.withParameterTypes(String.class, Integer.class)
42+
.withParameterValues(ParameterMatcher.ANYVALUE, ParameterMatcher.ANYVALUE)
43+
.returning('any params result')
44+
.defineStub(true);
45+
46+
// Act
47+
String result1 = mockObject.methodWithParameters('value1', 42);
48+
String result2 = mockObject.methodWithParameters('value2', 999);
49+
50+
// Assert
51+
Assert.areEqual(
52+
'any params result',
53+
result1,
54+
'Should return mocked result with any parameters (1)'
55+
);
56+
Assert.areEqual(
57+
'any params result',
58+
result2,
59+
'Should return mocked result with any parameters (2)'
60+
);
61+
}
62+
63+
@IsTest
64+
static void testMixedExactAndAnyMatchers() {
65+
// Arrange
66+
TestClass mockObject = (TestClass) new Stub.Builder(TestClass.class)
67+
// First stub with exact first parameter and ANY second parameter
68+
.mockingMethodCall('methodWithParameters')
69+
.withParameterTypes(String.class, Integer.class)
70+
.withParameterValues('exact', ParameterMatcher.ANYVALUE)
71+
.returning('exact first, any second')
72+
// Second stub with ANY first parameter and exact second parameter
73+
.mockingMethodCall('methodWithParameters')
74+
.withParameterTypes(String.class, Integer.class)
75+
.withParameterValues(ParameterMatcher.ANYVALUE, 42)
76+
.returning('any first, exact second')
77+
.defineStub(true);
78+
79+
// Act & Assert
80+
Assert.areEqual(
81+
'exact first, any second',
82+
mockObject.methodWithParameters('exact', 999),
83+
'Should match first stub with exact first parameter'
84+
);
85+
86+
Assert.areEqual(
87+
'any first, exact second',
88+
mockObject.methodWithParameters('different', 42),
89+
'Should match second stub with exact second parameter'
90+
);
91+
}
92+
}

sfdx-project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"name": "ApexKit",
99
"namespace": "",
1010
"sfdcLoginUrl": "https://login.salesforce.com",
11-
"sourceApiVersion": "58.0"
11+
"sourceApiVersion": "63.0"
1212
}

0 commit comments

Comments
 (0)