No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
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");import 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.ws.ConnectionException;
13
14public class RunTestListener {
15 private static final String CHANNEL = "/systemTopic/TestResult";
16 private SoapConnection conn;
17
18 public RunTestListener(BayeuxClient client, SoapConnection conn) {
19 this.conn = conn;
20 System.out.println("Subscribing for channel: " + CHANNEL);
21 client.getChannel(CHANNEL).subscribe(new MessageListener() {
22 @Override
23 public void onMessage(ClientSessionChannel channel, Message message) {
24 HashMap data = (HashMap) message.getData();
25 HashMap sobject = (HashMap) data.get("sobject");
26 String id = (String) sobject.get("Id");
27 System.out.println("\nAysncApexJob " + id);
28 getTestQueueItems(id);
29 }
30 });
31 }
32
33 public void runTests(String[] apexTestClassIds) {
34 if (apexTestClassIds.length == 0) {
35 System.out.println("No test to run");
36 return;
37 }
38 System.out.println("Running async test run");
39 String ids = apexTestClassIds[0];
40 for (int i = 1; i < apexTestClassIds.length; i++) {
41 ids += ","+apexTestClassIds[i];
42 }
43 try {
44 conn.runTestsAsynchronous(ids);
45 } catch (ConnectionException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 }
49 }
50 private void getTestQueueItems(String asyncApexJobId) {
51 try {
52 QueryResult res = conn
53 .query("SELECT Id, Status, ApexClassId FROM ApexTestQueueItem WHERE ParentJobId = '"
54 + asyncApexJobId + "'");
55 if (res.getSize() > 0) {
56 for (SObject o : res.getRecords()) {
57 ApexTestQueueItem atqi = (ApexTestQueueItem) o;
58 System.out.println("\tApexTestQueueItem - "+atqi.getStatus());
59 if (atqi.getStatus().equals("Completed")) {
60 getApexTestResults(atqi.getId());
61 }
62 }
63 } else {
64 System.out.println("No queued items for " + asyncApexJobId);
65 }
66 } catch (ConnectionException e) {
67 e.printStackTrace();
68 }
69 }
70
71 private void getApexTestResults(String apexTestQueueItemId) {
72 try {
73 QueryResult res = conn
74 .query("SELECT StackTrace,Message, AsyncApexJobId,MethodName, Outcome,ApexClassId FROM ApexTestResult WHERE QueueItemId = '"
75 + apexTestQueueItemId + "'");
76 if (res.getSize() > 0) {
77 for (SObject o : res.getRecords()) {
78 ApexTestResult atr = (ApexTestResult) o;
79 System.out.println("\tTest result for "
80 + atr.getApexClassId() + "." + atr.getMethodName());
81 String msg = atr.getOutcome().equals("Fail") ? " - "
82 + atr.getMessage() + " " + atr.getStackTrace() : "";
83 System.out.println("\t\tTest " + atr.getOutcome() + msg);
84 }
85 } else {
86 System.out.println("No Test Results for " + apexTestQueueItemId);
87 }
88 } catch (ConnectionException e) {
89 // TODO Auto-generated catch block
90 e.printStackTrace();
91 }
92 }
93}