-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ApexUnitTestClassShouldHaveAsserts.xml
185 lines (166 loc) · 6.63 KB
/
ApexUnitTestClassShouldHaveAsserts.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>Problematic apex unit test - no assert calls</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
@isTest
public class Foo {
public static testMethod void testSomething() {
Account a = null;
// This is better than having a NullPointerException
// System.assertNotEquals(a, null, 'account not found');
a.toString();
}
}
]]></code>
</test-code>
<test-code>
<description>#1543 [apex] ApexUnitTestClassShouldHaveAsserts assumes APEX is case sensitive</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
@isTest
public class Foo {
public static testMethod void testSomething() {
Account a = null;
SYSTEM.AssertNotEquals(a, null, 'account not found');
}
}
]]></code>
</test-code>
<test-code>
<description>#1521 [apex] ApexUnitTestClassShouldHaveAsserts: Parsing error on APEX class: expected one element but was: <BlockStatement, BlockStatement></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
@isTest
private class C2_Assignment_Report_Job_Test {
public static testMethod void testCreateAssignmentsForDifferentRoles() {
User u = C2_Test_Utils.getWFUser('testWfc', true);
System.runAs(u) {
C2_Assignment_Report_Data_Aggregate_Job job = new C2_Assignment_Report_Data_Aggregate_Job();
String query = job.start(null).getQuery();
C2_Receiver_Unit__c ru = new C2_Receiver_Unit__c(Name='RU1');
insert ru;
List<C2_Case__c> cases = new List<C2_Case__c>();
cases.add(new C2_Case__c(Name='TT0000000', Queue__c = 'PA-CSA', Status__c=C2_Case_Util.STATUS_NEW, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
cases.add(new C2_Case__c(Name='TT0000001', Queue__c = 'PA-CSA', Status__c=C2_Case_Util.STATUS_NEW, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
cases.add(new C2_Case__c(Name='TT0000002', Queue__c = 'PA-CSA', Status__c=C2_Case_Util.STATUS_NEW, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
cases.add(new C2_Case__c(Name='TT0000011', Queue__c = 'DSA', Status__c=C2_Case_Util.STATUS_PACSA_COMPLETE, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
cases.add(new C2_Case__c(Name='TT0000012', Queue__c = 'DSA', Status__c=C2_Case_Util.STATUS_PACSA_COMPLETE, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
cases.add(new C2_Case__c(Name='TT0000021', Queue__c = 'QR-CSA', Status__c=C2_Case_Util.STATUS_DSA_COMPLETE, PA_CSA__c=u.Id, DSA__c=u.Id, QR_CSA__c=u.Id, Receiver_Unit__c=ru.Id));
insert cases;
List<C2_Case__c> scope = Database.query(query);
Test.startTest();
job.execute(null, scope);
Test.stopTest();
List<C2_Case_Assign_Report_Data__c> caseEventEntries =
[select Num_Cases_Assign__c, Num_Cases_Not_Completed__c, Num_Cases_Reassigned__c, Num_Cases_Completed__c, User__c, Role__c
from C2_Case_Assign_Report_Data__c];
System.assertEquals(3, caseEventEntries.size());
Map<String, Integer> userIdWithRole2ReportData = getCaseEventEntriesMap(caseEventEntries);
System.assertEquals(3, userIdWithRole2ReportData.get(createUserRoleKey(u.Id, 'PA-CSA')));
System.assertEquals(2, userIdWithRole2ReportData.get(createUserRoleKey(u.Id, 'DSA')));
System.assertEquals(1, userIdWithRole2ReportData.get(createUserRoleKey(u.Id, 'QR-CSA')));
}
}
}
]]></code>
</test-code>
<test-code>
<description>#1089 [apex] ApexUnitTestClassShouldHaveAsserts: Verify use of additionalAssertMethodPattern, positive test</description>
<rule-property name="additionalAssertMethodPattern">(MyAssert\.\w+|verify\w+)</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
@isTest
public class Foo {
public static testMethod void testAssertIsTrue() {
MyAssert.isTrue(someCondition);
}
public static testMethod void testLocalVerify() {
verifyState();
}
private static void verifyState() {
}
}
]]></code>
</test-code>
<test-code>
<description>#1089 [apex] ApexUnitTestClassShouldHaveAsserts: Verify use of additionalAssertMethodPattern, negative test</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>3,7</expected-linenumbers>
<code><![CDATA[
@isTest
public class Foo {
public static testMethod void testAssertIsTrue() {
MyAssert.isTrue(someCondition);
}
public static testMethod void testLocalVerify() {
verifyState();
}
private static void verifyState() {
}
}
]]></code>
</test-code>
<test-code>
<description>[apex] ApexUnitTestClassShouldHaveAssertsRule: Support new Assert class (Apex v56.0) #4097</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
@isTest
public class Foo {
@isTest
static void testAreEqual() {
String sub = 'abcde'.substring(2);
Assert.areEqual('cde', sub);
}
@isTest
static void testAreNotEqual() {
String sub = 'abcde'.substring(2);
Assert.areNotEqual('xyz', sub);
}
@isTest
static void testFail() {
try {
SomeClass.methodUnderTest();
Assert.fail();
} catch (DmlException ex) {
// Add assertions here about the expected exception
}
}
@isTest
static void testIsFalse() {
Boolean containsCode = 'Salesforce'.contains('code');
Assert.isFalse(containsCode);
}
@isTest
static void testIsInstanceOf() {
Account o = new Account();
Assert.isInstanceOfType(o, Account.class);
}
@isTest
static void testIsNotInstanceOf() {
Contact con = new Contact();
Assert.isNotInstanceOfType(con, Account.class);
}
@isTest
static void testIsNotNull() {
String myString = 'value';
Assert.isNotNull(myString);
}
@isTest
static void testIsNull() {
String myString = null;
Assert.isNull(myString);
}
@isTest
static void testIsTrue() {
Boolean containsForce = 'Salesforce'.contains('force');
Assert.isTrue(containsForce);
}
}
]]></code>
</test-code>
</test-data>