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.apache.commons.lang3.StringUtils;
3import org.cometd.bayeux.Message;
4import org.cometd.bayeux.client.ClientSessionChannel;
5import org.cometd.bayeux.client.ClientSessionChannel.MessageListener;
6import org.cometd.client.BayeuxClient;
7
8import com.sforce.soap.tooling.ApexTestQueueItem;
9import com.sforce.soap.tooling.ApexTestResult;
10import com.sforce.soap.tooling.QueryResult;
11import com.sforce.soap.tooling.SObject;
12import com.sforce.soap.tooling.SoapConnection;
13import com.sforce.soap.tooling.TestLevel;
14import com.sforce.ws.ConnectionException;
15
16public class RunTestListener {
17 private static final String CHANNEL = "/systemTopic/TestResult";
18 private SoapConnection conn;
19
20 public RunTestListener(BayeuxClient client, SoapConnection conn) {
21 this.conn = conn;
22 System.out.println("Subscribing for channel: " + CHANNEL);
23 client.getChannel(CHANNEL).subscribe(new MessageListener() {
24 @Override
25 public void onMessage(ClientSessionChannel channel, Message message) {
26 HashMap data = (HashMap) message.getData();
27 HashMap sobject = (HashMap) data.get("sobject");
28 String id = (String) sobject.get("Id");
29 System.out.println("\nAysncApexJob " + id);
30 getTestQueueItems(id);
31 }
32 });
33 }
34
35 public void runTests(String[] apexTestClassIds, String[] apexTestSuiteIds,
36 Integer maxFailedTests, String[] apexTestClassNames, String[] apexTestSuiteNames) {
37
38 // Classes or suites are required; maxFailedTests is optional
39 if (apexTestClassIds == null && apexTestSuiteIds == null
40 && apexTestClassNames == null && apexTestSuiteNames == null) {
41 System.out.println("No tests to run");
42 return;
43 }
44 String classIds = StringUtils.join(apexTestClassIds,", ");
45
46 String suiteIds = StringUtils.join(apexTestSuiteIds,", ");
47 String classNames = StringUtils.join(apexTestClassNames,", ");
48
49 String suiteNames = StringUtils.join(apexTestSuiteNames,", ");
50
51 try {
52 System.out.println("Running async test run");
53 conn.runTestsAsynchronous(classIds, suiteIds, maxFailedTests,
54 TestLevel.RunSpecifiedTests, classNames, suiteNames);
55 } catch (ConnectionException e) {
56 e.printStackTrace();
57 }
58 }
59
60 private void getTestQueueItems(String asyncApexJobId) {
61 try {
62 QueryResult res = conn
63 .query("SELECT Id, Status, ApexClassId FROM ApexTestQueueItem
64 WHERE ParentJobId = '" + asyncApexJobId + "'");
65 if (res.getSize() > 0) {
66 for (SObject o : res.getRecords()) {
67 ApexTestQueueItem atqi = (ApexTestQueueItem) o;
68 System.out.println("\tApexTestQueueItem - "+atqi.getStatus());
69 if (atqi.getStatus().equals("Completed")) {
70 getApexTestResults(atqi.getId());
71 }
72 }
73 } else {
74 System.out.println("No queued items for " + asyncApexJobId);
75 }
76 } catch (ConnectionException e) {
77 e.printStackTrace();
78 }
79 }
80
81 private void getApexTestResults(String apexTestQueueItemId) {
82 try {
83 QueryResult res = conn
84 .query("SELECT StackTrace,Message, AsyncApexJobId,MethodName, Outcome,ApexClassId
85 FROM ApexTestResult WHERE QueueItemId = '" + apexTestQueueItemId + "'");
86 if (res.getSize() > 0) {
87 for (SObject o : res.getRecords()) {
88 ApexTestResult atr = (ApexTestResult) o;
89 System.out.println("\tTest result for "
90 + atr.getApexClassId() + "." + atr.getMethodName());
91 String msg = atr.getOutcome().equals("Fail") ? " - "
92 + atr.getMessage() + " " + atr.getStackTrace() : "";
93 System.out.println("\t\tTest " + atr.getOutcome() + msg);
94 }
95 } else {
96 System.out.println("No Test Results for " + apexTestQueueItemId);
97 }
98 } catch (ConnectionException e) {
99 e.printStackTrace();
100 }
101 }
102}