Newer Version Available
ApexTestQueueItem
Supported SOAP API Calls
create(), describeSObjects(), query(), retrieve(), update(), upsert()
Supported REST API HTTP Methods
Query, GET, POST, PATCH
Fields
| Field Name | Details |
|---|---|
| ApexClassId |
|
| Status |
|
| ExtendedStatus |
|
| ParentJobId |
|
| TestRunResultID |
|
Usage
Insert an ApexTestQueueItem object to place its corresponding Apex class in the Apex job queue for execution. The Apex job executes the test methods in the class.
- You have already set up a Java client application for Streaming API. This example uses the org.cometd.client.BayeuxClient created in the Java Client code example in the Streaming API Developer Guide.
- You have a logged in com.sforce.soap.tooling.SoapConnection. For examples, see the SOAP API Developer Guide.
1import java.util.HashMap;
2import org.cometd.bayeux.Message;
3import org.cometd.bayeux.client.ClientSessionChannel;
4import org.cometd.bayeux.client.ClientSessionChannel.MessageListener;
5import org.cometd.client.BayeuxClient;
6
7import com.sforce.soap.tooling.ApexTestQueueItem;
8import com.sforce.soap.tooling.ApexTestResult;
9import com.sforce.soap.tooling.QueryResult;
10import com.sforce.soap.tooling.SObject;
11import com.sforce.soap.tooling.SoapConnection;
12import com.sforce.soap.tooling.TestLevel;
13import com.sforce.ws.ConnectionException;
14
15public class RunTestListener {
16 private static final String CHANNEL = "/systemTopic/TestResult";
17 private SoapConnection conn;
18
19 public RunTestListener(BayeuxClient client, SoapConnection conn) {
20 this.conn = conn;
21 System.out.println("Subscribing for channel: " + CHANNEL);
22 client.getChannel(CHANNEL).subscribe(new MessageListener() {
23 @Override
24 public void onMessage(ClientSessionChannel channel, Message message) {
25 HashMap data = (HashMap) message.getData();
26 HashMap sobject = (HashMap) data.get("sobject");
27 String id = (String) sobject.get("Id");
28 System.out.println("\nAysncApexJob " + id);
29 getTestQueueItems(id);
30 }
31 });
32 }
33
34 public void runTests(String[] apexTestClassIds, String[] apexTestSuiteIds, Integer maxFailedTests) {
35 // apexTestClassIds or apexTestSuiteIds is required; maxFailedTests is optional
36 if (apexTestClassIds.length == 0 && apexTestSuiteIds.length == 0) {
37 System.out.println("No test to run");
38 return;
39 }
40 String classIds = null;
41 if (apexTestClassIds.length > 0) {
42 classIds = apexTestClassIds[0];
43 for (int i = 1; i < apexTestClassIds.length; i++) {
44 classIds += "," + apexTestClassIds[i];
45 }
46 }
47 String suiteIds = null;
48 if (apexTestSuiteIds.length > 0) {
49 suiteIds = apexTestSuiteIds[0];
50 for (int i = 1; i < apexTestSuiteIds.length; i++) {
51 suiteIds += "," + apexTestSuiteIds[i];
52 }
53 }
54 try {
55 System.out.println("Running async test run");
56 conn.runTestsAsynchronous(classIds, suiteIds, maxFailedTests, TestLevel.RunSpecifiedTests);
57 } catch (ConnectionException e) {
58 e.printStackTrace();
59 }
60 }
61
62 private void getTestQueueItems(String asyncApexJobId) {
63 try {
64 QueryResult res = conn
65 .query("SELECT Id, Status, ApexClassId FROM ApexTestQueueItem WHERE ParentJobId = '"
66 + asyncApexJobId + "'");
67 if (res.getSize() > 0) {
68 for (SObject o : res.getRecords()) {
69 ApexTestQueueItem atqi = (ApexTestQueueItem) o;
70 System.out.println("\tApexTestQueueItem - "+atqi.getStatus());
71 if (atqi.getStatus().equals("Completed")) {
72 getApexTestResults(atqi.getId());
73 }
74 }
75 } else {
76 System.out.println("No queued items for " + asyncApexJobId);
77 }
78 } catch (ConnectionException e) {
79 e.printStackTrace();
80 }
81 }
82
83 private void getApexTestResults(String apexTestQueueItemId) {
84 try {
85 QueryResult res = conn
86 .query("SELECT StackTrace,Message, AsyncApexJobId,MethodName, Outcome,ApexClassId FROM ApexTestResult WHERE QueueItemId = '"
87 + apexTestQueueItemId + "'");
88 if (res.getSize() > 0) {
89 for (SObject o : res.getRecords()) {
90 ApexTestResult atr = (ApexTestResult) o;
91 System.out.println("\tTest result for "
92 + atr.getApexClassId() + "." + atr.getMethodName());
93 String msg = atr.getOutcome().equals("Fail") ? " - "
94 + atr.getMessage() + " " + atr.getStackTrace() : "";
95 System.out.println("\t\tTest " + atr.getOutcome() + msg);
96 }
97 } else {
98 System.out.println("No Test Results for " + apexTestQueueItemId);
99 }
100 } catch (ConnectionException e) {
101 e.printStackTrace();
102 }
103 }
104}