Use this call to both compile and test the Apex you specify with a single call. Production organizations (not a Developer Edition or Sandbox Edition) must use this call instead of compileClasses() or compileTriggers().
All specified tests must pass, otherwise data is not saved to the database. If this call is invoked in a production organization, the RunTestsRequest property of the CompileAndTestRequest is ignored, and all unit tests defined in the organization are run and must pass.
Sample Code—Java
Note that the following example sets checkOnly to true so that this class is compiled and tested, but the classes are not saved to the database.
1{2 CompileAndTestRequest request;3 CompileAndTestResult result = null;45 String triggerBody = "trigger t1 on Account (before insert){ " +6 " for(Account a:Trigger.new){ " +7 " a.description = 't1_UPDATE';}" +8 "}";910 String testClassBody = "@isTest private class TestT1{" +11 " // Test for the trigger" +12 " public static testmethod void test1(){" +13 " Account a = new Account(name='TEST');" +14 " insert(a);" +15 " a = [select id,description from Account where id=:a.id];" +16 " System.assert(a.description.contains('t1_UPDATE'));" +17 " }" +18 " // Test for the class" +19 " public static testmethod void test2(){" +20 " String s = C1.method1();" +21 " System.assert(s=='HELLO');" +22 " }" +23 "}";2425 String classBody = "public class C1{" +26 " public static String s ='HELLO';" +27 " public static String method1(){" +28 " return(s);" +29 " }" +30 "}";3132 request = new CompileAndTestRequest();3334 request.setClasses(new String[]{classBody, testClassBody});35 request.setTriggers(new String[]{triggerBody});36 request.setCheckOnly(true);3738 try {39 result = apexBinding.compileAndTest(request);40 } catch (RemoteException e) {41 System.out.println("An unexpected error occurred: " + e.getMessage());42 }43 assert (result.isSuccess());44}