-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathApex Testing- Create Test Data for Apex Tests
29 lines (24 loc) · 1.67 KB
/
Apex Testing- Create Test Data for Apex Tests
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
Challenge 12:
Create a contact test factory.
Create an Apex class that returns a list of contacts based on two incoming parameters: one for the number of contacts to generate, and the other for the last name. The list should NOT be inserted into the system, only returned. The first name should be dynamically generated and should be unique for each contact record in the list.
The Apex class must be called 'RandomContactFactory' and be in the public scope.
The Apex class should NOT use the @isTest annotation.
The Apex class must have a public static method called 'generateRandomContacts' (without the @testMethod annotation).
The 'generateRandomContacts' method must accept an integer as the first parameter, and a string as the second. The first parameter controls the number of contacts being generated, the second is the last name of the contacts generated.
The 'generateRandomContacts' method should have a return type of List<Contact>.
The 'generateRandomContacts' method must be capable of consistently generating contacts with unique first names.
For example, the 'generateRandomContacts' might return first names based on iterated number (i.e. 'Test 1','Test 2').
The 'generateRandomContacts' method should not insert the contact records into the database.
Solution:
1.RandomContactFactory.apxc
public class RandomContactFactory {
public static List<Contact> generateRandomContacts(Integer NumberofContacts, String lName){
List<Contact> con = new List<Contact>();
for(Integer i=0; i<NumberofContacts; i++){
lName = 'Test'+i;
Contact c = new Contact(FirstName=lName, LastName=lName);
con.add(c);
}
return con;
}
}