Newer Version Available
ApexTestQueueItem
Supported SOAP API Calls
create(), describeSObjects(), query(), retrieve(), update(), upsert()
Supported REST API HTTP Methods
Query, GET, POST, PATCH
Fields
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.
The example RunTestListener.java class below
subscribes to the TestResult system topic and prints out the test results using
ApexTestQueueItem and ApexTestResult. The example assumes the following:
- 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's Guide.
- You have a logged in com.sforce.soap.tooling.SoapConnection. For examples, see the SOAP API Developer's Guide.
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import java.util.HashMap;
18import org.cometd.bayeux.Message;
19import org.cometd.bayeux.client.ClientSessionChannel;
20import org.cometd.bayeux.client.ClientSessionChannel.MessageListener;
21import org.cometd.client.BayeuxClient;
22
23import com.sforce.soap.tooling.ApexTestQueueItem;
24import com.sforce.soap.tooling.ApexTestResult;
25import com.sforce.soap.tooling.QueryResult;
26import com.sforce.soap.tooling.SObject;
27import com.sforce.soap.tooling.SoapConnection;
28import com.sforce.ws.ConnectionException;
29
30public class RunTestListener {
31 private static final String CHANNEL = "/systemTopic/TestResult";
32 private SoapConnection conn;
33
34 public RunTestListener(BayeuxClient client, SoapConnection conn) {
35 this.conn = conn;
36 System.out.println("Subscribing for channel: " + CHANNEL);
37 client.getChannel(CHANNEL).subscribe(new MessageListener() {
38 @Override
39 public void onMessage(ClientSessionChannel channel, Message message) {
40 HashMap data = (HashMap) message.getData();
41 HashMap sobject = (HashMap) data.get("sobject");
42 String id = (String) sobject.get("Id");
43 System.out.println("\nAysncApexJob " + id);
44 getTestQueueItems(id);
45 }
46 });
47 }
48
49 public void runTests(String[] apexTestClassIds) {
50 if (apexTestClassIds.length == 0) {
51 System.out.println("No test to run");
52 return;
53 }
54 System.out.println("Running async test run");
55 String ids = apexTestClassIds[0];
56 for (int i = 1; i < apexTestClassIds.length; i++) {
57 ids += ","+apexTestClassIds[i];
58 }
59 try {
60 conn.runTestsAsynchronous(ids);
61 } catch (ConnectionException e) {
62 // TODO Auto-generated catch block
63 e.printStackTrace();
64 }
65 }
66 private void getTestQueueItems(String asyncApexJobId) {
67 try {
68 QueryResult res = conn
69 .query("SELECT Id, Status, ApexClassId FROM ApexTestQueueItem WHERE ParentJobId = '"
70 + asyncApexJobId + "'");
71 if (res.getSize() > 0) {
72 for (SObject o : res.getRecords()) {
73 ApexTestQueueItem atqi = (ApexTestQueueItem) o;
74 System.out.println("\tApexTestQueueItem - "+atqi.getStatus());
75 if (atqi.getStatus().equals("Completed")) {
76 getApexTestResults(atqi.getId());
77 }
78 }
79 } else {
80 System.out.println("No queued items for " + asyncApexJobId);
81 }
82 } catch (ConnectionException e) {
83 e.printStackTrace();
84 }
85 }
86
87 private void getApexTestResults(String apexTestQueueItemId) {
88 try {
89 QueryResult res = conn
90 .query("SELECT StackTrace,Message, AsyncApexJobId,MethodName, Outcome,ApexClassId FROM ApexTestResult WHERE QueueItemId = '"
91 + apexTestQueueItemId + "'");
92 if (res.getSize() > 0) {
93 for (SObject o : res.getRecords()) {
94 ApexTestResult atr = (ApexTestResult) o;
95 System.out.println("\tTest result for "
96 + atr.getApexClassId() + "." + atr.getMethodName());
97 String msg = atr.getOutcome().equals("Fail") ? " - "
98 + atr.getMessage() + " " + atr.getStackTrace() : "";
99 System.out.println("\t\tTest " + atr.getOutcome() + msg);
100 }
101 } else {
102 System.out.println("No Test Results for " + apexTestQueueItemId);
103 }
104 } catch (ConnectionException e) {
105 // TODO Auto-generated catch block
106 e.printStackTrace();
107 }
108 }
109}